Skip to content

Commit

Permalink
Overwrite scoped registry
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMeah committed Oct 27, 2022
1 parent 16352bb commit 5f599ca
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5,109 deletions.
62 changes: 61 additions & 1 deletion __tests__/authutil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ describe('authutil tests', () => {
for (const line of contents.split(os.EOL)) {
let parts = line.split('=');
if (parts.length == 2) {
rc[parts[0].trim()] = parts[1].trim();
let key = parts[0].trim();

if (rc[key]) {
throw new Error(`Duplicate key ${key} in ${rcFile}`);
}

rc[key] = parts[1].trim();
}
}
return rc;
Expand Down Expand Up @@ -132,4 +138,58 @@ describe('authutil tests', () => {
expect(rc['always-auth']).toBe('false');
expect(process.env.NODE_AUTH_TOKEN).toEqual('foobar');
});

describe('Existing .npmrc', () => {
it('should overwrite registry', async () => {
fs.copyFileSync(path.join(__dirname, 'data/.npmrc'), rcFile);
expect(fs.statSync(rcFile)).toBeDefined();

let rc = readRcFile(rcFile);
expect(rc['registry']).toBe('http://example.com');
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
let updatedRc = readRcFile(rcFile);
expect(updatedRc['registry']).toBe('https://registry.npmjs.org/');
expect(updatedRc['always-auth']).toBe('true');
});

it('should overwrite scoped registry', async () => {
process.env['INPUT_SCOPE'] = 'myScope';

fs.copyFileSync(path.join(__dirname, 'data/.npmrc-scoped'), rcFile);
expect(fs.statSync(rcFile)).toBeDefined();

let rc = readRcFile(rcFile);
expect(rc['@myscope:registry']).toBe('http://example.com');

await auth.configAuthentication('https://registry.npmjs.org/', 'true');

let updatedRc = readRcFile(rcFile);
expect(updatedRc['@myscope:registry']).toBe(
'https://registry.npmjs.org/'
);
expect(updatedRc['always-auth']).toBe('true');
});

it('should not delete registry when scoped', async () => {
process.env['INPUT_SCOPE'] = 'myScope';

fs.copyFileSync(
path.join(__dirname, 'data/.npmrc-scoped-with-registry'),
rcFile
);
expect(fs.statSync(rcFile)).toBeDefined();

let rc = readRcFile(rcFile);
expect(rc['@myscope:registry']).toBe('http://example.com');

await auth.configAuthentication('https://registry.npmjs.org/', 'true');

let updatedRc = readRcFile(rcFile);
expect(updatedRc['registry']).toBe('http://base.com');
expect(updatedRc['@myscope:registry']).toBe(
'https://registry.npmjs.org/'
);
expect(updatedRc['always-auth']).toBe('true');
});
});
});
1 change: 1 addition & 0 deletions __tests__/data/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=http://example.com
1 change: 1 addition & 0 deletions __tests__/data/.npmrc-scoped
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@myscope:registry=http://example.com
2 changes: 2 additions & 0 deletions __tests__/data/.npmrc-scoped-with-registry
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
registry=http://base.com
@myscope:registry=http://example.com
Loading

0 comments on commit 5f599ca

Please sign in to comment.