Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit c71a77e

Browse files
committed
test(trdl-actions): cover setup-app action
Signed-off-by: Alexandr Zaytsev <[email protected]>
1 parent b907aa1 commit c71a77e

File tree

7 files changed

+275
-16
lines changed

7 files changed

+275
-16
lines changed

setup-app/src/add.test.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { beforeEach, describe, it, jest } from '@jest/globals'
2+
import * as core from '../../test/mocks/core'
3+
import { trdlCli } from '../../test/mocks/trdl-cli'
4+
import { getAddArgs, preset } from './preset'
5+
import { AddArgs, ListItem } from '../../lib/trdl-cli'
6+
7+
// Mocks should be declared before the module being tested is imported.
8+
jest.unstable_mockModule('@actions/core', () => core)
9+
10+
// The module being tested should be imported dynamically. This ensures that the
11+
// mocks are used in place of any actual dependencies.
12+
const { parseInputs, Do } = await import('./add')
13+
14+
describe('setup-app/src/add.ts', function () {
15+
describe('parseInputs', function () {
16+
it('should work if force is not passed and required=false', function () {
17+
core.getInput.mockReturnValue('')
18+
core.getBooleanInput.mockReturnValue(false)
19+
expect(parseInputs(false)).toEqual({
20+
force: false,
21+
repo: '',
22+
url: '',
23+
rootVersion: '',
24+
rootSha512: ''
25+
})
26+
})
27+
it('should work if force is passed and required=true', function () {
28+
const value = 'input value'
29+
core.getInput.mockReturnValue(value)
30+
core.getBooleanInput.mockReturnValue(true)
31+
expect(parseInputs(true)).toEqual({
32+
force: true,
33+
repo: value,
34+
url: value,
35+
rootVersion: value,
36+
rootSha512: value
37+
})
38+
})
39+
})
40+
describe('Do', function () {
41+
let addArgs: AddArgs
42+
let listItem: ListItem
43+
beforeEach(function () {
44+
addArgs = getAddArgs(preset.werf)
45+
listItem = {
46+
name: addArgs.repo,
47+
url: addArgs.url,
48+
default: 'some default'
49+
}
50+
})
51+
it('should add app if preset=unknown and bin-path found', async function () {
52+
addArgs.repo = 'some repo'
53+
addArgs.url = 'some url'
54+
addArgs.rootVersion = 'some root version'
55+
addArgs.rootSha512 = 'some root sha512'
56+
57+
core.getInput.mockReturnValueOnce(addArgs.repo)
58+
core.getInput.mockReturnValueOnce(addArgs.url)
59+
core.getInput.mockReturnValueOnce(addArgs.rootVersion)
60+
core.getInput.mockReturnValueOnce(addArgs.rootSha512)
61+
62+
core.getBooleanInput.mockReturnValueOnce(false)
63+
64+
trdlCli.list.mockResolvedValueOnce([])
65+
66+
await Do(trdlCli, preset.unknown)
67+
68+
expect(trdlCli.mustExist).toHaveBeenCalled()
69+
expect(trdlCli.list).toHaveBeenCalled()
70+
expect(trdlCli.add).toHaveBeenCalledWith(addArgs)
71+
})
72+
it('should throw err if preset=werf, app found, force=false and found.url != input.url', async function () {
73+
core.getInput.mockReturnValueOnce('')
74+
core.getBooleanInput.mockReturnValueOnce(false)
75+
76+
listItem.url = 'another url'
77+
78+
trdlCli.list.mockResolvedValueOnce([listItem])
79+
80+
const expectedErr = new Error(
81+
`Already added repo.url=${listItem.url} is not matched with given input.url=${addArgs.url}. Use the force input to overwrite.`
82+
)
83+
await expect(Do(trdlCli, preset.werf)).rejects.toThrow(expectedErr)
84+
85+
expect(trdlCli.mustExist).toHaveBeenCalled()
86+
expect(trdlCli.list).toHaveBeenCalled()
87+
})
88+
it('should do nothing if preset=werf, app found, force=false and found.url == input.url', async function () {
89+
core.getInput.mockReturnValue('')
90+
core.getBooleanInput.mockReturnValueOnce(false)
91+
92+
trdlCli.list.mockResolvedValueOnce([listItem])
93+
94+
await Do(trdlCli, preset.werf)
95+
96+
expect(trdlCli.mustExist).toHaveBeenCalled()
97+
expect(trdlCli.list).toHaveBeenCalled()
98+
expect(trdlCli.add).not.toHaveBeenCalled()
99+
})
100+
it('should force add app if preset=werf, app found, force=true', async function () {
101+
core.getInput.mockReturnValue('')
102+
core.getBooleanInput.mockReturnValueOnce(true)
103+
104+
trdlCli.list.mockResolvedValueOnce([listItem])
105+
106+
await Do(trdlCli, preset.werf)
107+
108+
expect(trdlCli.mustExist).toHaveBeenCalled()
109+
expect(trdlCli.list).toHaveBeenCalled()
110+
expect(trdlCli.remove).toHaveBeenCalledWith(addArgs)
111+
expect(trdlCli.add).toHaveBeenCalledWith(addArgs)
112+
})
113+
})
114+
})

setup-app/src/add.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ interface inputs extends AddArgs {
77
force: boolean
88
}
99

10-
function parseInputs(required: boolean): inputs {
10+
export function parseInputs(required: boolean): inputs {
1111
return {
12-
force: getBooleanInput('force', { required }),
12+
force: getBooleanInput('force'),
1313
repo: getInput('repo', { required }),
1414
url: getInput('url', { required }),
1515
rootVersion: getInput('root-version', { required }),

setup-app/src/preset.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, jest, it } from '@jest/globals'
2+
import * as core from '../../test/mocks/core'
3+
4+
// Mocks should be declared before the module being tested is imported.
5+
jest.unstable_mockModule('@actions/core', () => core)
6+
7+
// The module being tested should be imported dynamically. This ensures that the
8+
// mocks are used in place of any actual dependencies.
9+
const { preset, parsePresetInput } = await import('./preset')
10+
11+
describe('setup-app/src/preset.ts', function () {
12+
describe('parsePresetInput', function () {
13+
it('should return unknown if preset not passed', function () {
14+
core.getInput.mockReturnValueOnce('')
15+
16+
const p = parsePresetInput()
17+
expect(p).toEqual(preset.unknown)
18+
})
19+
it('should throw err if preset is passed but not in enum', function () {
20+
const p = 'some'
21+
core.getInput.mockReturnValueOnce(p)
22+
expect(parsePresetInput).toThrow(
23+
`preset "${p}" not found. Available presets: ${Object.values(preset).join(', ')}`
24+
)
25+
})
26+
it('should return preset otherwise', function () {
27+
const p = 'werf'
28+
core.getInput.mockReturnValueOnce(p)
29+
expect(parsePresetInput()).toEqual(p)
30+
})
31+
})
32+
})

setup-app/src/preset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function parsePresetInput(): preset {
6060
const p = (getInput('preset') as preset) || preset.unknown
6161

6262
if (!(p in preset)) {
63-
throw new Error(`preset "${p}" not found. Available presets: ${Object.values(preset).join(' ,')}`)
63+
throw new Error(`preset "${p}" not found. Available presets: ${Object.values(preset).join(', ')}`)
6464
}
6565

6666
return p

setup-app/src/use.test.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { beforeEach, describe, it, jest } from '@jest/globals'
2+
import * as core from '../../test/mocks/core'
3+
import { UpdateArgs } from '../../lib/trdl-cli'
4+
import { trdlCli } from '../../test/mocks/trdl-cli'
5+
import { getUpdateArgs, preset } from './preset'
6+
7+
// Mocks should be declared before the module being tested is imported.
8+
jest.unstable_mockModule('@actions/core', () => core)
9+
10+
// The module being tested should be imported dynamically. This ensures that the
11+
// mocks are used in place of any actual dependencies.
12+
const { parseInputs, formatTrdlUseEnv, Do } = await import('./use')
13+
14+
describe('setup-app/src/use.ts', function () {
15+
describe('parseInputs', function () {
16+
it('should work if force is not passed and required=false and w/o optional fields', function () {
17+
core.getInput.mockReturnValue('')
18+
core.getBooleanInput.mockReturnValue(false)
19+
expect(parseInputs(false)).toEqual({
20+
force: false,
21+
repo: '',
22+
group: ''
23+
})
24+
})
25+
it('should work if force is passed and required=true', function () {
26+
const value = 'input value'
27+
core.getInput.mockReturnValue(value)
28+
core.getBooleanInput.mockReturnValue(true)
29+
expect(parseInputs(true)).toEqual({
30+
force: true,
31+
repo: value,
32+
group: value,
33+
channel: value
34+
})
35+
})
36+
})
37+
describe('formatTrdlUseEnv', function () {
38+
it('should work w/o channel', function () {
39+
const args: UpdateArgs = {
40+
repo: 'some repo',
41+
group: 'some group'
42+
}
43+
expect(formatTrdlUseEnv(args)).toEqual({
44+
key: 'TRDL_USE_SOME-REPO_GROUP_CHANNEL',
45+
value: `${args.group} `
46+
})
47+
})
48+
it('should work with channel', function () {
49+
const args: UpdateArgs = {
50+
repo: 'some repo',
51+
group: 'some group',
52+
channel: 'some channel'
53+
}
54+
expect(formatTrdlUseEnv(args)).toEqual({
55+
key: 'TRDL_USE_SOME-REPO_GROUP_CHANNEL',
56+
value: `${args.group} ${args.channel}`
57+
})
58+
})
59+
})
60+
describe('Do', function () {
61+
let updArgs: UpdateArgs
62+
beforeEach(function () {
63+
updArgs = { ...getUpdateArgs(preset.werf) }
64+
})
65+
it('should update app in background if preset=unknown and appPath is found', async function () {
66+
updArgs.repo = 'some repo'
67+
updArgs.group = 'some group'
68+
updArgs.channel = 'some channel'
69+
70+
core.getInput.mockReturnValueOnce(updArgs.channel)
71+
core.getInput.mockReturnValueOnce(updArgs.repo)
72+
core.getInput.mockReturnValueOnce(updArgs.group)
73+
74+
core.getBooleanInput.mockReturnValueOnce(false)
75+
76+
const appPath = '/app/path'
77+
trdlCli.binPath.mockResolvedValueOnce(appPath)
78+
79+
await Do(trdlCli, preset.unknown)
80+
81+
expect(trdlCli.mustExist).toHaveBeenCalled()
82+
expect(trdlCli.binPath).toHaveBeenCalledWith(updArgs)
83+
expect(trdlCli.binPath).toHaveBeenCalledTimes(1)
84+
expect(trdlCli.update).toHaveBeenCalledWith(updArgs, { inBackground: true })
85+
expect(core.exportVariable).toHaveBeenCalledWith(
86+
'TRDL_USE_SOME-REPO_GROUP_CHANNEL',
87+
`${updArgs.group} ${updArgs.channel}`
88+
)
89+
expect(core.addPath).toHaveBeenCalledWith(appPath)
90+
})
91+
it('should update app in foreground if preset=werf and appPath is not found', async function () {
92+
core.getInput.mockReturnValueOnce(updArgs.channel as string)
93+
core.getInput.mockReturnValueOnce(updArgs.repo)
94+
core.getInput.mockReturnValueOnce(updArgs.group)
95+
96+
core.getBooleanInput.mockReturnValueOnce(false)
97+
98+
const appPath = '/app/path'
99+
trdlCli.binPath.mockResolvedValueOnce('')
100+
trdlCli.binPath.mockResolvedValueOnce(appPath)
101+
102+
await Do(trdlCli, preset.werf)
103+
104+
expect(trdlCli.mustExist).toHaveBeenCalled()
105+
expect(trdlCli.binPath).toHaveBeenCalledWith(updArgs)
106+
expect(trdlCli.binPath).toHaveBeenCalledTimes(2)
107+
expect(trdlCli.update).toHaveBeenCalledWith(updArgs, { inBackground: false })
108+
expect(core.exportVariable).toHaveBeenCalledWith(
109+
'TRDL_USE_WERF_GROUP_CHANNEL',
110+
`${updArgs.group} ${updArgs.channel}`
111+
)
112+
expect(core.addPath).toHaveBeenCalledWith(appPath)
113+
})
114+
})
115+
})

setup-app/src/use.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ interface envVar {
1313
value: string
1414
}
1515

16-
function parseInputs(required: boolean): inputs {
16+
export function parseInputs(required: boolean): inputs {
1717
const channel = getInput('channel')
1818
return {
19-
force: getBooleanInput('force', { required }),
19+
force: getBooleanInput('force'),
2020
repo: getInput('repo', { required }),
2121
group: getInput('group', { required }),
2222
...(channel !== '' ? { channel } : {}) // optional field
@@ -32,12 +32,12 @@ function mapInputsToCmdArgs(inputs: inputs): UpdateArgs {
3232
}
3333
}
3434

35-
function formatTrdlUseEnv(args: UpdateArgs): envVar {
35+
export function formatTrdlUseEnv(args: UpdateArgs): envVar {
3636
const slugOpts = {
3737
strict: true
3838
}
3939
return {
40-
key: format('TRDL_USE_%s_GROUP_CHANNEL', slugify(args.repo, slugOpts)),
40+
key: format('TRDL_USE_%s_GROUP_CHANNEL', slugify(args.repo, slugOpts).toUpperCase()),
4141
value: format(`%s %s`, args.group, args.channel || '')
4242
}
4343
}
@@ -58,18 +58,15 @@ export async function Do(trdlCli: TrdlCli, p: preset) {
5858
let appPath = await trdlCli.binPath(args)
5959
debug(format(`"trdl bin-path" application path=%s`, appPath))
6060

61-
if (!appPath) {
62-
const opts = { inBackground: false }
63-
info(format('Updating application via "trdl update" with args=%o and options=%o.', args, opts))
64-
await trdlCli.update(args, opts)
61+
const hasAppPath = appPath !== ''
6562

63+
const opts = { inBackground: hasAppPath }
64+
info(format('Updating application via "trdl update" with args=%o and options=%o.', args, opts))
65+
await trdlCli.update(args, opts)
66+
67+
if (!hasAppPath) {
6668
appPath = await trdlCli.binPath(args)
6769
debug(format(`"trdl bin-path" application path=%s`, appPath))
68-
} else {
69-
const opts = { inBackground: true }
70-
info(format('Updating application via "trdl update" with args=%o and options=%o.', args, opts))
71-
72-
await trdlCli.update(args, opts)
7370
}
7471

7572
const trdlUseEnv = formatTrdlUseEnv(args)

test/mocks/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const setFailed = jest.fn<typeof core.setFailed>()
1111
export const addPath = jest.fn<typeof core.addPath>()
1212
export const startGroup = jest.fn<typeof core.startGroup>()
1313
export const endGroup = jest.fn<typeof core.endGroup>()
14+
export const exportVariable = jest.fn<typeof core.exportVariable>()
1415

1516
export const platform = Object.create(
1617
{},

0 commit comments

Comments
 (0)