Skip to content

Commit 56eacf9

Browse files
author
Bryan Clark
committed
move required parameters to auth module
username and password are required from within the auth module now. Update the tests to ensure this is the case.
1 parent 56b5af7 commit 56eacf9

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

__tests__/auth.test.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import io = require('@actions/io');
22
import fs = require('fs');
3+
import os = require('os');
34
import path = require('path');
4-
import child_process = require('child_process');
55

6-
const m2Dir = path.join(__dirname, '.m2');
7-
const settingsFile = path.join(m2Dir, 'settings.xml');
6+
// make the os.homedir() call be local to the tests
7+
jest.doMock('os', () => {
8+
return {
9+
homedir: jest.fn(() => __dirname)
10+
};
11+
});
812

913
import * as auth from '../src/auth';
1014

15+
const m2Dir = path.join(__dirname, auth.M2_DIR);
16+
const settingsFile = path.join(m2Dir, auth.SETTINGS_FILE);
17+
1118
describe('auth tests', () => {
12-
beforeAll(async () => {
19+
beforeEach(async () => {
1320
await io.rmRF(m2Dir);
1421
}, 300000);
1522

@@ -21,7 +28,7 @@ describe('auth tests', () => {
2128
}
2229
}, 100000);
2330

24-
it('Creates settings.xml file with username and password', async () => {
31+
it('creates settings.xml with username and password', async () => {
2532
const username = 'bluebottle';
2633
const password = 'SingleOrigin';
2734

@@ -33,4 +40,21 @@ describe('auth tests', () => {
3340
auth.generate(username, password)
3441
);
3542
}, 100000);
43+
44+
it('does not create settings.xml without username and / or password', async () => {
45+
await auth.configAuthentication('FOO', '');
46+
47+
expect(fs.existsSync(m2Dir)).toBe(false);
48+
expect(fs.existsSync(settingsFile)).toBe(false);
49+
50+
await auth.configAuthentication('', 'BAR');
51+
52+
expect(fs.existsSync(m2Dir)).toBe(false);
53+
expect(fs.existsSync(settingsFile)).toBe(false);
54+
55+
await auth.configAuthentication('', ''); // BAZ!!!
56+
57+
expect(fs.existsSync(m2Dir)).toBe(false);
58+
expect(fs.existsSync(settingsFile)).toBe(false);
59+
}, 100000);
3660
});

src/auth.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,40 @@ import * as path from 'path';
44
import * as core from '@actions/core';
55
import * as io from '@actions/io';
66

7+
export const M2_DIR = '.m2';
8+
export const SETTINGS_FILE = 'settings.xml';
9+
710
export async function configAuthentication(username: string, password: string) {
8-
const directory: string = path.join(os.homedir(), '.m2');
9-
await io.mkdirP(directory);
10-
await write(directory, generate(username, password));
11+
if (username && password) {
12+
core.debug(`configAuthentication with ${username} and a password`);
13+
const directory: string = path.join(os.homedir(), M2_DIR);
14+
await io.mkdirP(directory);
15+
core.debug(`created directory ${directory}`);
16+
await write(directory, generate(username, password));
17+
} else {
18+
core.debug(
19+
`no auth without username: ${username} and password: ${password}`
20+
);
21+
}
1122
}
1223

1324
// only exported for testing purposes
14-
export function generate(
15-
username = '${actions.username}',
16-
password = '${actions.password}'
17-
) {
18-
return `<settings>
19-
<servers>
20-
<server>
21-
<username>${username}</username>
22-
<password>${password}</password>
23-
</server>
24-
</servers>
25-
</settings>
26-
`;
25+
export function generate(username: string, password: string) {
26+
return `
27+
<settings>
28+
<servers>
29+
<server>
30+
<username>${username}</username>
31+
<password>${password}</password>
32+
</server>
33+
</servers>
34+
</settings>
35+
`;
2736
}
2837

2938
async function write(directory: string, settings: string) {
30-
return fs.writeFileSync(path.join(directory, 'settings.xml'), settings);
39+
const options = {encoding: 'utf-8'};
40+
const location = path.join(directory, SETTINGS_FILE);
41+
core.debug(`writing ${location}`);
42+
return fs.writeFileSync(location, settings, options);
3143
}

src/setup-java.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ async function run() {
1818
const username = core.getInput('username', {required: false});
1919
const password = core.getInput('password', {required: false});
2020

21-
if (username && password) {
22-
await auth.configAuthentication(username, password);
23-
}
21+
await auth.configAuthentication(username, password);
2422

2523
const matchersPath = path.join(__dirname, '..', '.github');
2624
console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);

0 commit comments

Comments
 (0)