|
1 | | -import * as io from '@actions/io'; |
2 | 1 | import * as fs from 'fs'; |
3 | 2 | import * as path from 'path'; |
| 3 | +import * as core from '@actions/core'; |
| 4 | +import * as io from '@actions/io'; |
4 | 5 | import * as auth from '../src/authutil'; |
5 | 6 |
|
6 | 7 | let rcFile: string; |
7 | 8 |
|
8 | | -describe('installer tests', () => { |
| 9 | +describe('authutil tests', () => { |
| 10 | + const _runnerDir = path.join(__dirname, 'runner'); |
| 11 | + |
| 12 | + let cnSpy: jest.SpyInstance; |
| 13 | + let logSpy: jest.SpyInstance; |
| 14 | + let dbgSpy: jest.SpyInstance; |
| 15 | + |
9 | 16 | beforeAll(async () => { |
10 | | - const tempDir = path.join( |
11 | | - __dirname, |
12 | | - 'runner', |
13 | | - path.join( |
14 | | - Math.random() |
15 | | - .toString(36) |
16 | | - .substring(7) |
17 | | - ), |
18 | | - 'temp' |
| 17 | + const randPath = path.join( |
| 18 | + Math.random() |
| 19 | + .toString(36) |
| 20 | + .substring(7) |
19 | 21 | ); |
| 22 | + const tempDir = path.join(_runnerDir, randPath, 'temp'); |
20 | 23 | await io.rmRF(tempDir); |
21 | 24 | await io.mkdirP(tempDir); |
22 | 25 | process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo'; |
23 | 26 | process.env['RUNNER_TEMP'] = tempDir; |
24 | 27 | rcFile = path.join(tempDir, '.npmrc'); |
25 | 28 | }, 100000); |
26 | 29 |
|
27 | | - beforeEach(() => { |
28 | | - if (fs.existsSync(rcFile)) { |
29 | | - fs.unlinkSync(rcFile); |
30 | | - } |
| 30 | + beforeEach(async () => { |
| 31 | + await io.rmRF(rcFile); |
| 32 | + // if (fs.existsSync(rcFile)) { |
| 33 | + // fs.unlinkSync(rcFile); |
| 34 | + // } |
31 | 35 | process.env['INPUT_SCOPE'] = ''; |
32 | | - }); |
| 36 | + |
| 37 | + // writes |
| 38 | + cnSpy = jest.spyOn(process.stdout, 'write'); |
| 39 | + logSpy = jest.spyOn(console, 'log'); |
| 40 | + dbgSpy = jest.spyOn(core, 'debug'); |
| 41 | + cnSpy.mockImplementation(line => { |
| 42 | + // uncomment to debug |
| 43 | + // process.stderr.write('write:' + line + '\n'); |
| 44 | + }); |
| 45 | + logSpy.mockImplementation(line => { |
| 46 | + // uncomment to debug |
| 47 | + // process.stderr.write('log:' + line + '\n'); |
| 48 | + }); |
| 49 | + dbgSpy.mockImplementation(msg => { |
| 50 | + // uncomment to see debug output |
| 51 | + // process.stderr.write(msg + '\n'); |
| 52 | + }); |
| 53 | + }, 100000); |
| 54 | + |
| 55 | + afterAll(async () => { |
| 56 | + if (_runnerDir) { |
| 57 | + await io.rmRF(_runnerDir); |
| 58 | + } |
| 59 | + }, 100000); |
33 | 60 |
|
34 | 61 | it('Sets up npmrc for npmjs', async () => { |
35 | 62 | await auth.configAuthentication('https://registry.npmjs.org/', 'false'); |
36 | | - expect(fs.existsSync(rcFile)).toBe(true); |
| 63 | + |
| 64 | + expect(fs.statSync(rcFile)).toBeDefined(); |
37 | 65 | expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot(); |
38 | 66 | }); |
39 | 67 |
|
40 | 68 | it('Appends trailing slash to registry', async () => { |
41 | 69 | await auth.configAuthentication('https://registry.npmjs.org', 'false'); |
42 | 70 |
|
43 | | - expect(fs.existsSync(rcFile)).toBe(true); |
| 71 | + expect(fs.statSync(rcFile)).toBeDefined(); |
44 | 72 | expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot(); |
45 | 73 | }); |
46 | 74 |
|
47 | 75 | it('Configures scoped npm registries', async () => { |
48 | 76 | process.env['INPUT_SCOPE'] = 'myScope'; |
49 | 77 | await auth.configAuthentication('https://registry.npmjs.org', 'false'); |
50 | 78 |
|
51 | | - expect(fs.existsSync(rcFile)).toBe(true); |
| 79 | + expect(fs.statSync(rcFile)).toBeDefined(); |
52 | 80 | expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot(); |
53 | 81 | }); |
54 | 82 |
|
55 | 83 | it('Automatically configures GPR scope', async () => { |
56 | 84 | await auth.configAuthentication('npm.pkg.github.com', 'false'); |
57 | 85 |
|
58 | | - expect(fs.existsSync(rcFile)).toBe(true); |
| 86 | + expect(fs.statSync(rcFile)).toBeDefined(); |
59 | 87 | expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot(); |
60 | 88 | }); |
61 | 89 |
|
62 | 90 | it('Sets up npmrc for always-auth true', async () => { |
63 | 91 | await auth.configAuthentication('https://registry.npmjs.org/', 'true'); |
64 | | - expect(fs.existsSync(rcFile)).toBe(true); |
| 92 | + expect(fs.statSync(rcFile)).toBeDefined(); |
65 | 93 | expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot(); |
66 | 94 | }); |
67 | 95 | }); |
0 commit comments