2020-05-02 23:06:05 +08:00
|
|
|
import os = require('os');
|
2020-03-10 23:51:57 +08:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2020-05-02 21:51:47 +08:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as io from '@actions/io';
|
2019-08-07 06:26:04 +08:00
|
|
|
import * as auth from '../src/authutil';
|
|
|
|
|
2020-03-10 23:51:57 +08:00
|
|
|
let rcFile: string;
|
|
|
|
|
2020-05-02 21:51:47 +08:00
|
|
|
describe('authutil tests', () => {
|
|
|
|
const _runnerDir = path.join(__dirname, 'runner');
|
|
|
|
|
|
|
|
let cnSpy: jest.SpyInstance;
|
|
|
|
let logSpy: jest.SpyInstance;
|
|
|
|
let dbgSpy: jest.SpyInstance;
|
|
|
|
|
2019-08-07 06:26:04 +08:00
|
|
|
beforeAll(async () => {
|
2020-05-02 21:51:47 +08:00
|
|
|
const randPath = path.join(
|
|
|
|
Math.random()
|
|
|
|
.toString(36)
|
|
|
|
.substring(7)
|
2020-03-10 23:51:57 +08:00
|
|
|
);
|
2020-05-02 21:51:47 +08:00
|
|
|
const tempDir = path.join(_runnerDir, randPath, 'temp');
|
2019-08-07 06:26:04 +08:00
|
|
|
await io.rmRF(tempDir);
|
|
|
|
await io.mkdirP(tempDir);
|
2020-03-10 23:51:57 +08:00
|
|
|
process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo';
|
|
|
|
process.env['RUNNER_TEMP'] = tempDir;
|
|
|
|
rcFile = path.join(tempDir, '.npmrc');
|
2019-08-07 06:26:04 +08:00
|
|
|
}, 100000);
|
|
|
|
|
2020-05-02 21:51:47 +08:00
|
|
|
beforeEach(async () => {
|
|
|
|
await io.rmRF(rcFile);
|
|
|
|
// if (fs.existsSync(rcFile)) {
|
|
|
|
// fs.unlinkSync(rcFile);
|
|
|
|
// }
|
2019-08-07 06:26:04 +08:00
|
|
|
process.env['INPUT_SCOPE'] = '';
|
2020-05-02 21:51:47 +08:00
|
|
|
|
|
|
|
// writes
|
|
|
|
cnSpy = jest.spyOn(process.stdout, 'write');
|
|
|
|
logSpy = jest.spyOn(console, 'log');
|
|
|
|
dbgSpy = jest.spyOn(core, 'debug');
|
|
|
|
cnSpy.mockImplementation(line => {
|
|
|
|
// uncomment to debug
|
|
|
|
// process.stderr.write('write:' + line + '\n');
|
|
|
|
});
|
|
|
|
logSpy.mockImplementation(line => {
|
|
|
|
// uncomment to debug
|
|
|
|
// process.stderr.write('log:' + line + '\n');
|
|
|
|
});
|
|
|
|
dbgSpy.mockImplementation(msg => {
|
|
|
|
// uncomment to see debug output
|
|
|
|
// process.stderr.write(msg + '\n');
|
|
|
|
});
|
|
|
|
}, 100000);
|
|
|
|
|
2020-05-02 22:45:32 +08:00
|
|
|
function dbg(message: string) {
|
|
|
|
process.stderr.write('dbg::' + message + '::\n');
|
|
|
|
}
|
|
|
|
|
2020-05-02 21:51:47 +08:00
|
|
|
afterAll(async () => {
|
|
|
|
if (_runnerDir) {
|
|
|
|
await io.rmRF(_runnerDir);
|
|
|
|
}
|
|
|
|
}, 100000);
|
2019-08-07 06:26:04 +08:00
|
|
|
|
2020-05-02 23:06:05 +08:00
|
|
|
function readRcFile(rcFile: string) {
|
|
|
|
let rc = {};
|
|
|
|
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
|
|
|
|
for (const line of contents.split(os.EOL)) {
|
|
|
|
let parts = line.split('=');
|
|
|
|
if (parts.length == 2) {
|
|
|
|
rc[parts[0].trim()] = parts[1].trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-08-07 06:26:04 +08:00
|
|
|
it('Sets up npmrc for npmjs', async () => {
|
2019-09-03 22:57:45 +08:00
|
|
|
await auth.configAuthentication('https://registry.npmjs.org/', 'false');
|
2020-05-02 21:51:47 +08:00
|
|
|
|
|
|
|
expect(fs.statSync(rcFile)).toBeDefined();
|
2020-05-02 22:45:32 +08:00
|
|
|
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
|
2020-05-02 23:06:05 +08:00
|
|
|
let rc = readRcFile(rcFile);
|
|
|
|
expect(rc["registry"]).toBe("https://registry.npmjs.org/");
|
|
|
|
expect(rc["always-auth"]).toBe("false");
|
2019-08-07 06:26:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Appends trailing slash to registry', async () => {
|
2019-09-03 22:57:45 +08:00
|
|
|
await auth.configAuthentication('https://registry.npmjs.org', 'false');
|
2019-08-07 06:26:04 +08:00
|
|
|
|
2020-05-02 21:51:47 +08:00
|
|
|
expect(fs.statSync(rcFile)).toBeDefined();
|
2020-05-02 23:06:05 +08:00
|
|
|
let rc = readRcFile(rcFile);
|
|
|
|
expect(rc["registry"]).toBe("https://registry.npmjs.org/");
|
|
|
|
expect(rc["always-auth"]).toBe("false");
|
2019-08-07 06:26:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Configures scoped npm registries', async () => {
|
|
|
|
process.env['INPUT_SCOPE'] = 'myScope';
|
2019-09-03 22:57:45 +08:00
|
|
|
await auth.configAuthentication('https://registry.npmjs.org', 'false');
|
2019-08-07 06:26:04 +08:00
|
|
|
|
2020-05-02 21:51:47 +08:00
|
|
|
expect(fs.statSync(rcFile)).toBeDefined();
|
2020-05-02 23:06:05 +08:00
|
|
|
let rc = readRcFile(rcFile);
|
|
|
|
expect(rc["@myscope:registry"]).toBe("https://registry.npmjs.org/");
|
|
|
|
expect(rc["always-auth"]).toBe("false");
|
2019-08-07 06:26:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Automatically configures GPR scope', async () => {
|
2019-09-03 22:57:45 +08:00
|
|
|
await auth.configAuthentication('npm.pkg.github.com', 'false');
|
2019-08-07 06:26:04 +08:00
|
|
|
|
2020-05-02 21:51:47 +08:00
|
|
|
expect(fs.statSync(rcFile)).toBeDefined();
|
2020-05-02 23:06:05 +08:00
|
|
|
let rc = readRcFile(rcFile);
|
|
|
|
expect(rc["@ownername:registry"]).toBe("npm.pkg.github.com/");
|
|
|
|
expect(rc["always-auth"]).toBe("false");
|
2019-08-07 06:26:04 +08:00
|
|
|
});
|
2019-09-03 22:57:45 +08:00
|
|
|
|
|
|
|
it('Sets up npmrc for always-auth true', async () => {
|
|
|
|
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
|
2020-05-02 21:51:47 +08:00
|
|
|
expect(fs.statSync(rcFile)).toBeDefined();
|
2020-05-02 23:06:05 +08:00
|
|
|
let rc = readRcFile(rcFile);
|
|
|
|
expect(rc["registry"]).toBe("https://registry.npmjs.org/");
|
|
|
|
expect(rc["always-auth"]).toBe("true");
|
2019-09-03 22:57:45 +08:00
|
|
|
});
|
2019-08-07 06:26:04 +08:00
|
|
|
});
|