2023-02-07 15:13:45 -08:00
import { isString } from "@github/actions-workflow-parser" ;
2023-01-23 16:02:13 -05:00
import { StringToken } from "@github/actions-workflow-parser/templates/tokens/string-token" ;
import { DescriptionProvider , hover , HoverConfig } from "./hover" ;
2022-11-30 14:39:21 -05:00
import { getPositionFromCursor } from "./test-utils/cursor-position" ;
2023-02-17 11:53:25 -08:00
import { testFileProvider } from "./test-utils/test-file-provider" ;
2022-11-21 17:21:40 -05:00
2023-02-02 08:57:26 -08:00
export function testHoverConfig ( tokenValue : string , tokenKey : string , description? : string ) {
2023-01-23 16:02:13 -05:00
return {
descriptionProvider : {
getDescription : async ( _ , token , __ ) => {
if ( ! isString ( token )) {
throw new Error ( "Test provider only supports string tokens" );
}
expect (( token as StringToken ). value ). toEqual ( tokenValue );
expect ( token . definition ! . key ). toEqual ( tokenKey );
return description ;
}
2023-02-17 11:53:25 -08:00
} satisfies DescriptionProvider ,
fileProvider : testFileProvider
2023-01-30 10:57:40 -08:00
} satisfies HoverConfig ;
2023-01-23 16:02:13 -05:00
}
2022-11-30 15:21:16 -05:00
describe ( "hover" , () => {
2022-12-02 15:23:15 -05:00
it ( "on a key" , async () => {
2022-11-30 14:39:21 -05:00
const input = `o|n: push
2022-11-21 17:21:40 -05:00
jobs:
build:
2022-11-30 14:39:21 -05:00
runs-on: [self-hosted]` ;
2022-11-30 15:21:16 -05:00
const result = await hover (... getPositionFromCursor ( input ));
2022-11-21 17:21:40 -05:00
expect ( result ). not . toBeUndefined ();
2023-01-13 12:32:08 -08:00
expect ( result ? . contents ). toContain ( "The GitHub event that triggers the workflow." );
2022-11-21 17:21:40 -05:00
});
2022-12-02 15:23:15 -05:00
it ( "on a value" , async () => {
2022-11-30 14:39:21 -05:00
const input = `on: pu|sh
2022-11-21 17:21:40 -05:00
jobs:
build:
2022-11-30 14:39:21 -05:00
runs-on: [self-hosted]` ;
2022-11-30 15:21:16 -05:00
const result = await hover (... getPositionFromCursor ( input ));
2022-11-30 14:39:21 -05:00
expect ( result ). not . toBeUndefined ();
expect ( result ? . contents ). toEqual ( "Runs your workflow when you push a commit or tag." );
});
2023-01-06 13:32:53 -08:00
it ( "on a parameter with a description" , async () => {
const input = `on: push
jobs:
build:
co|ntinue-on-error: false` ;
const result = await hover (... getPositionFromCursor ( input ));
expect ( result ). not . toBeUndefined ();
expect ( result ? . contents ). toEqual (
"Prevents a workflow run from failing when a job fails. Set to true to allow a workflow run to pass when this job fails.\n\n" +
2023-01-06 15:38:08 -08:00
"**Context:** github, inputs, vars, needs, strategy, matrix"
2023-01-06 13:32:53 -08:00
);
});
it ( "on a parameter with its own type" , async () => {
const input = `on: push
jobs:
build:
pe|rmissions: read-all` ;
const result = await hover (... getPositionFromCursor ( input ));
expect ( result ). not . toBeUndefined ();
2023-01-13 12:32:08 -08:00
expect ( result ? . contents ). toContain (
"You can use `permissions` to modify the default permissions granted to the `GITHUB_TOKEN`"
2023-01-06 13:32:53 -08:00
);
});
2023-01-09 13:56:12 -08:00
it ( "property values are not overwritten" , async () => {
const input1 = `on: push
jobs:
build:
ti|meout-minutes: 10
cancel-timeout-minutes: 10` ;
const result1 = await hover (... getPositionFromCursor ( input1 ));
expect ( result1 ). not . toBeUndefined ();
const input2 = `on: push
jobs:
build:
timeout-minutes: 10
ca|ncel-timeout-minutes: 10` ;
const result2 = await hover (... getPositionFromCursor ( input2 ));
expect ( result2 ). not . toBeUndefined ();
expect ( result1 ? . contents ). not . toEqual ( result2 ? . contents );
});
2022-12-02 15:23:15 -05:00
it ( "on a value in a sequence" , async () => {
2023-01-06 15:38:08 -08:00
const input = `on: [pull_request,
2022-11-30 14:39:21 -05:00
pu|sh]
jobs:
build:
runs-on: [self-hosted]` ;
2022-11-30 15:21:16 -05:00
const result = await hover (... getPositionFromCursor ( input ));
2022-11-30 14:39:21 -05:00
expect ( result ). not . toBeUndefined ();
expect ( result ? . contents ). toEqual ( "Runs your workflow when you push a commit or tag." );
2022-11-08 17:00:59 -08:00
});
2023-01-23 15:12:16 -08:00
it ( "on a cron schedule" , async () => {
const input = `on:
schedule:
- cron: '0,30 0|,12 * * *'
` ;
const result = await hover (... getPositionFromCursor ( input ));
expect ( result ). not . toBeUndefined ();
expect ( result ? . contents ). toEqual (
"Runs at 0 and 30 minutes past the hour, at 00:00 and 12:00\n\n" +
2023-01-23 16:25:30 -08:00
"Actions schedules run at most every 5 minutes. " +
"[Learn more](https://docs.github.com/actions/using-workflows/workflow-syntax-for-github-actions#onschedule)"
2023-01-23 15:12:16 -08:00
);
});
2023-01-23 17:50:54 -08:00
it ( "on a cron mapping key" , async () => {
2023-01-23 16:53:29 -08:00
const input = `on:
schedule:
- c|ron: '0 0 * * *'
` ;
const result = await hover (... getPositionFromCursor ( input ));
expect ( result ). not . toBeUndefined ();
expect ( result ? . contents ). toEqual ( "" );
});
2023-01-23 15:12:16 -08:00
it ( "on an invalid cron schedule" , async () => {
const input = `on:
schedule:
- cron: '0 0 |* * * * *'
` ;
const result = await hover (... getPositionFromCursor ( input ));
expect ( result ). not . toBeUndefined ();
2023-01-23 17:50:54 -08:00
expect ( result ? . contents ). toEqual ( "" );
2023-01-23 15:12:16 -08:00
});
2023-01-23 16:20:59 -05:00
it ( "shows context inherited from parent nodes" , async () => {
const input = `
on: push
jobs:
build:
runs-on: [self-hosted]
steps:
- uses: actions/checkout@v2
with:
ref|: main
` ;
const result = await hover (... getPositionFromCursor ( input ));
expect ( result ). not . toBeUndefined ();
// The `ref` is a `string` definition and inherits the context from `step-with`
2023-01-30 10:57:40 -08:00
const expected =
"**Context:** github, inputs, vars, needs, strategy, matrix, secrets, steps, job, runner, env, hashFiles(1,255)" ;
2023-01-23 16:20:59 -05:00
expect ( result ? . contents ). toEqual ( expected );
});
2022-11-08 17:00:59 -08:00
});
2023-01-23 16:02:13 -05:00
describe ( "hover with description provider" , () => {
it ( "uses the description provider" , async () => {
const input = `
on: push
jobs:
build:
runs-on: [self-hosted]
steps:
- uses: actions/checkout@v2
with:
ref|: main
` ;
2023-01-30 10:57:40 -08:00
const result = await hover (
... getPositionFromCursor ( input ),
testHoverConfig ( "ref" , "string" , "The branch, tag or SHA to checkout." )
);
2023-01-23 16:02:13 -05:00
expect ( result ). not . toBeUndefined ();
2023-01-23 16:20:59 -05:00
2023-01-30 10:57:40 -08:00
const expected =
"The branch, tag or SHA to checkout.\n\n" +
"**Context:** github, inputs, vars, needs, strategy, matrix, secrets, steps, job, runner, env, hashFiles(1,255)" ;
2023-01-23 16:20:59 -05:00
expect ( result ? . contents ). toEqual ( expected );
2023-01-23 16:02:13 -05:00
});
it ( "falls back to the token description" , async () => {
const input = `
on: push
jobs:
build:
runs-on: [self-hosted]
steps:
- uses|: actions/checkout@v2
` ;
2023-03-06 16:40:54 -05:00
const result = await hover (... getPositionFromCursor ( input ), testHoverConfig ( "uses" , "step-uses" , undefined ));
2023-01-23 16:02:13 -05:00
expect ( result ). not . toBeUndefined ();
2023-01-30 10:57:40 -08:00
expect ( result ? . contents ). toEqual (
"Selects an action to run as part of a step in your job. An action is a reusable unit of code. You can use an action defined in the same repository as the workflow, a public repository, or in a published Docker container image."
);
2023-01-23 16:02:13 -05:00
});
});