2023-02-24 08:53:51 -08:00
import { data , DescriptionDictionary } from "@actions/expressions" ;
import { format } from "@actions/expressions/funcs/format" ;
2023-02-02 08:57:26 -08:00
import { Hover } from "vscode-languageserver-types" ;
import { ContextProviderConfig } from "./context-providers/config" ;
import { hover } from "./hover" ;
import { registerLogger } from "./log" ;
import { getPositionFromCursor } from "./test-utils/cursor-position" ;
import { TestLogger } from "./test-utils/logger" ;
2023-03-06 15:16:26 -08:00
import { clearCache } from "./utils/workflow-cache" ;
2023-02-02 08:57:26 -08:00
const contextProviderConfig : ContextProviderConfig = {
2023-03-20 13:33:58 -04:00
getContext : ( context : string ) => {
2023-02-02 08:57:26 -08:00
switch ( context ) {
case "github" :
2023-03-20 13:33:58 -04:00
return Promise . resolve (
new DescriptionDictionary (
{
key : "event" ,
2023-02-02 08:57:26 -08:00
value : new data . StringData ( "push" ),
2023-03-20 13:33:58 -04:00
description : "The event that triggered the workflow"
},
{
key : "test" ,
value : new DescriptionDictionary ({
key : "name" ,
value : new data . StringData ( "push" ),
description : "Name for the test"
}),
description : "Test dictionary"
}
)
2023-02-02 08:57:26 -08:00
);
}
2023-03-20 13:33:58 -04:00
return Promise . resolve ( undefined );
2023-02-02 08:57:26 -08:00
}
};
registerLogger ( new TestLogger ());
2023-03-06 11:52:21 -08:00
beforeEach (() => {
2023-03-06 15:16:26 -08:00
clearCache ();
2023-03-06 11:52:21 -08:00
});
2023-02-02 08:57:26 -08:00
describe ( "hover.expressions" , () => {
it ( "context access" , async () => {
const input = `on: push
run-name: \ ${ { github . even | t } }
jobs:
build:
runs-on: [self-hosted]` ;
const result = await hover (... getPositionFromCursor ( input ), {
contextProviderConfig
});
expect ( result ). toEqual < Hover >({
contents : "The event that triggered the workflow" ,
range : {
start : { line : 1 , character : 14 },
end : { line : 1 , character : 26 }
}
});
});
it ( "context" , async () => {
const input = `on: push
run-name: \ ${ { git | hub . event } }
jobs:
build:
runs-on: [self-hosted]` ;
const result = await hover (... getPositionFromCursor ( input ), {
contextProviderConfig
});
expect ( result ). toEqual < Hover >({
contents :
"Information about the workflow run. For more information, see [`github` context](https://docs.github.com/actions/learn-github-actions/contexts#github-context)." ,
range : {
start : { line : 1 , character : 14 },
end : { line : 1 , character : 20 }
}
});
});
it ( "multiple expressions" , async () => {
const input = `on: push
run-name: \ ${ { git | hub . event } }- \ ${ { github . event } }
jobs:
build:
runs-on: [self-hosted]` ;
const result = await hover (... getPositionFromCursor ( input ), {
contextProviderConfig
});
expect ( result ). toEqual < Hover >({
contents :
"Information about the workflow run. For more information, see [`github` context](https://docs.github.com/actions/learn-github-actions/contexts#github-context)." ,
range : {
start : { line : 1 , character : 14 },
end : { line : 1 , character : 20 }
}
});
});
it ( "multi-line expression" , async () => {
const input = `on: push
jobs:
build:
runs-on: [self-hosted]
steps:
- run: |
echo 'hello'
echo ' \ ${ { github . test . na | me } }
echo 'world'
echo ' \ ${ { github . event . test } }` ;
const result = await hover (... getPositionFromCursor ( input , 1 ), {
contextProviderConfig
});
expect ( result ). toEqual < Hover >({
contents : "Name for the test" ,
range : {
start : { line : 7 , character : 18 },
end : { line : 7 , character : 34 }
}
});
});
2023-03-20 17:06:25 -04:00
it ( "built-in function" , async () => {
const input = `on: push
run-name: \ ${ { form | at ( 'Run {0}' , github . run_id ) } }
jobs:
build:
runs-on: [self-hosted]` ;
const result = await hover (... getPositionFromCursor ( input ), {
contextProviderConfig
});
expect ( result ). toEqual < Hover >({
contents : format.description || "" ,
range : {
start : { line : 1 , character : 14 },
end : { line : 1 , character : 20 }
}
});
});
it ( "schema-defined function" , async () => {
const input = `on: push
jobs:
build:
if: alwa|ys()
runs-on: [self-hosted]` ;
const result = await hover (... getPositionFromCursor ( input ), {
contextProviderConfig
});
expect ( result ). toEqual < Hover >({
contents :
"Causes the step to always execute, and returns `true`, even when canceled. The `always` expression is best used at the step level or on tasks that you expect to run even when a job is canceled. For example, you can use `always` to send logs even when a job is canceled." ,
range : {
start : { line : 3 , character : 11 },
end : { line : 3 , character : 17 }
}
});
});
it ( "schema-defined function with different casing" , async () => {
const input = `on: push
jobs:
build:
runs-on: [self-hosted]
steps:
- run: echo \ ${ { hash | Files ( 'test.txt' ) } }` ;
const result = await hover (... getPositionFromCursor ( input ), {
contextProviderConfig
});
expect ( result ). toEqual < Hover >({
contents :
"Returns a single hash for the set of files that matches the `path` pattern. You can provide a single `path` pattern or multiple `path` patterns separated by commas. The `path` is relative to the `GITHUB_WORKSPACE` directory and can only include files inside of the `GITHUB_WORKSPACE`." ,
range : {
start : { line : 5 , character : 22 },
end : { line : 5 , character : 31 }
}
});
});
2023-02-02 08:57:26 -08:00
});