Skip to content

Commit

Permalink
fix: install in action dist directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche committed Apr 26, 2022
1 parent 4c79631 commit 21ddb90
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/util/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ export function install(packages: string[], log: (msg: string) => void): Promise
if (missing.length) {
log(`Install ${JSON.stringify(missing)}`)

// if logging function is core.info, we're in debug/test
const isDebug = log === info

const args = ['install', '--no-save']
if (log !== info) { // if logging function is core.info, we're in debug/test
if (!isDebug) {
args.push('--silent')
}
args.push('--', ...missing)

return spawn('npm', args, {})
return spawn('npm', args, {
cwd: __dirname,
})
}

return Promise.resolve('')
Expand Down
18 changes: 16 additions & 2 deletions test/util/install.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { SpawnOptions } from 'child_process'
import path from 'path'
import { install } from '../../src/util/install'

let resolveMock: jest.MockedFunction<(name: string) => string>
jest.mock('../../src/util/resolve', () => ({
resolve: (name: string) => resolveMock(name),
}))

let spawnMock: jest.MockedFn<(cmd: string, args: string[]) => Promise<string>>
let spawnMock: jest.MockedFn<(cmd: string, args: string[], opt: SpawnOptions) => Promise<string>>
jest.mock('../../src/util/spawn', () => ({
spawn: (cmd: string, args: string[]) => spawnMock(cmd, args),
spawn: (cmd: string, args: string[], opt: SpawnOptions) => spawnMock(cmd, args, opt),
}))

test('skip present modules', async () => {
Expand Down Expand Up @@ -42,3 +44,15 @@ test('install missing modules', async () => {
expect(spawnMock.mock.calls[0][1]).toContain('bar')
expect(log).toEqual([expect.stringContaining('Install ["foo","bar"]')])
})

test('install in dist', async () => {
spawnMock = jest.fn()

await install(['foo'], () => undefined)

expect(spawnMock).toBeCalledTimes(1)
expect(spawnMock.mock.calls[0][2]).toEqual(expect.objectContaining({
// In test this should be the directory of the `install` util
cwd: path.resolve(__dirname, '../../src/util'),
}))
})

0 comments on commit 21ddb90

Please sign in to comment.