2020-02-24 09:49:55 +00:00
|
|
|
import path from 'path';
|
|
|
|
import fs from 'fs';
|
|
|
|
import {
|
|
|
|
getHomeDir,
|
|
|
|
getWorkDirName,
|
2020-07-21 02:15:53 +00:00
|
|
|
createDir,
|
2020-02-24 09:49:55 +00:00
|
|
|
addNoJekyll,
|
2020-03-16 00:00:19 +00:00
|
|
|
addCNAME,
|
|
|
|
skipOnFork
|
2020-02-24 09:49:55 +00:00
|
|
|
} from '../src/utils';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetModules();
|
|
|
|
});
|
|
|
|
|
|
|
|
// afterEach(() => {
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
async function getTime(): Promise<string> {
|
|
|
|
const date = new Date();
|
|
|
|
const unixTime = date.getTime();
|
|
|
|
return `${unixTime}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('getHomeDir()', () => {
|
|
|
|
test('get home directory name', async () => {
|
|
|
|
let test = '';
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
test = 'C:\\Users\\runneradmin';
|
|
|
|
} else {
|
|
|
|
test = `${process.env.HOME}`;
|
|
|
|
}
|
|
|
|
const expected = await getHomeDir();
|
|
|
|
expect(test).toMatch(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getWorkDirName()', () => {
|
|
|
|
test('get work directory name', async () => {
|
|
|
|
let home = '';
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
home = 'C:\\Users\\runneradmin';
|
|
|
|
} else {
|
|
|
|
home = `${process.env.HOME}`;
|
|
|
|
}
|
|
|
|
const unixTime = await getTime();
|
|
|
|
const expected = path.join(home, `actions_github_pages_${unixTime}`);
|
|
|
|
const test = await getWorkDirName(`${unixTime}`);
|
|
|
|
expect(test).toMatch(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-21 02:15:53 +00:00
|
|
|
describe('createDir()', () => {
|
|
|
|
test('create a directory', async () => {
|
2020-02-24 09:49:55 +00:00
|
|
|
const unixTime = await getTime();
|
|
|
|
const workDirName = await getWorkDirName(`${unixTime}`);
|
2020-07-21 02:15:53 +00:00
|
|
|
await createDir(workDirName);
|
2020-02-24 09:49:55 +00:00
|
|
|
const test = fs.existsSync(workDirName);
|
|
|
|
expect(test).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
async function getWorkDir(): Promise<string> {
|
|
|
|
const unixTime = await getTime();
|
|
|
|
let workDir = '';
|
|
|
|
workDir = await getWorkDirName(`${unixTime}`);
|
2020-07-21 02:15:53 +00:00
|
|
|
await createDir(workDir);
|
2020-02-24 09:49:55 +00:00
|
|
|
return workDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('addNoJekyll()', () => {
|
2020-08-02 06:11:20 +00:00
|
|
|
test('add .nojekyll', async () => {
|
2020-02-24 09:49:55 +00:00
|
|
|
let workDir = '';
|
|
|
|
(async (): Promise<void> => {
|
|
|
|
workDir = await getWorkDir();
|
|
|
|
})();
|
|
|
|
const filepath = path.join(workDir, '.nojekyll');
|
|
|
|
|
2020-08-02 06:11:20 +00:00
|
|
|
await addNoJekyll(workDir, false);
|
2020-03-06 15:41:30 +00:00
|
|
|
const test = fs.existsSync(filepath);
|
|
|
|
expect(test).toBe(true);
|
|
|
|
|
|
|
|
fs.unlinkSync(filepath);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('.nojekyll already exists', async () => {
|
|
|
|
let workDir = '';
|
|
|
|
(async (): Promise<void> => {
|
|
|
|
workDir = await getWorkDir();
|
|
|
|
})();
|
|
|
|
const filepath = path.join(workDir, '.nojekyll');
|
|
|
|
fs.closeSync(fs.openSync(filepath, 'w'));
|
|
|
|
|
2020-08-02 06:11:20 +00:00
|
|
|
await addNoJekyll(workDir, false);
|
2020-03-06 15:41:30 +00:00
|
|
|
const test = fs.existsSync(filepath);
|
|
|
|
expect(test).toBe(true);
|
2020-02-24 09:49:55 +00:00
|
|
|
|
|
|
|
fs.unlinkSync(filepath);
|
|
|
|
});
|
|
|
|
|
2020-08-02 06:11:20 +00:00
|
|
|
test('not add .nojekyll disable_nojekyll', async () => {
|
2020-02-24 09:49:55 +00:00
|
|
|
let workDir = '';
|
|
|
|
(async (): Promise<void> => {
|
|
|
|
workDir = await getWorkDir();
|
|
|
|
})();
|
|
|
|
const filepath = path.join(workDir, '.nojekyll');
|
|
|
|
|
2020-08-02 06:11:20 +00:00
|
|
|
await addNoJekyll(workDir, true);
|
2020-03-06 15:41:30 +00:00
|
|
|
const test = fs.existsSync(filepath);
|
|
|
|
expect(test).toBe(false);
|
2020-02-24 09:49:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('addCNAME()', () => {
|
|
|
|
test('add CNAME', async () => {
|
|
|
|
let workDir = '';
|
|
|
|
(async (): Promise<void> => {
|
|
|
|
workDir = await getWorkDir();
|
|
|
|
})();
|
|
|
|
const filepath = path.join(workDir, 'CNAME');
|
|
|
|
|
|
|
|
await addCNAME(workDir, 'github.com');
|
2020-03-06 15:41:30 +00:00
|
|
|
const test = fs.readFileSync(filepath, 'utf8');
|
|
|
|
expect(test).toMatch('github.com');
|
2020-02-24 09:49:55 +00:00
|
|
|
|
|
|
|
fs.unlinkSync(filepath);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('do nothing', async () => {
|
|
|
|
let workDir = '';
|
|
|
|
(async (): Promise<void> => {
|
|
|
|
workDir = await getWorkDir();
|
|
|
|
})();
|
|
|
|
const filepath = path.join(workDir, 'CNAME');
|
|
|
|
|
|
|
|
await addCNAME(workDir, '');
|
2020-03-06 15:41:30 +00:00
|
|
|
const test = fs.existsSync(filepath);
|
|
|
|
expect(test).toBe(false);
|
2020-02-24 09:49:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('CNAME already exists', async () => {
|
|
|
|
let workDir = '';
|
|
|
|
(async (): Promise<void> => {
|
|
|
|
workDir = await getWorkDir();
|
|
|
|
})();
|
|
|
|
const filepath = path.join(workDir, 'CNAME');
|
|
|
|
|
|
|
|
await addCNAME(workDir, 'github.io');
|
|
|
|
await addCNAME(workDir, 'github.com');
|
2020-03-06 15:41:30 +00:00
|
|
|
const test = fs.readFileSync(filepath, 'utf8');
|
|
|
|
expect(test).toMatch('github.io');
|
2020-02-24 09:49:55 +00:00
|
|
|
|
|
|
|
fs.unlinkSync(filepath);
|
|
|
|
});
|
|
|
|
});
|
2020-03-16 00:00:19 +00:00
|
|
|
|
|
|
|
describe('skipOnFork()', () => {
|
|
|
|
test('return false on upstream', async () => {
|
|
|
|
const test = await skipOnFork(false, 'token', '', '');
|
|
|
|
expect(test).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('return false on fork with github_token', async () => {
|
|
|
|
const test = await skipOnFork(true, 'token', '', '');
|
|
|
|
expect(test).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('return false on fork with deploy_key', async () => {
|
|
|
|
const test = await skipOnFork(true, '', 'deploy_key', '');
|
|
|
|
expect(test).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('return false on fork with personal_token', async () => {
|
|
|
|
const test = await skipOnFork(true, '', '', 'personal_token');
|
|
|
|
expect(test).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('return true on fork with no tokens', async () => {
|
|
|
|
const test = await skipOnFork(true, '', '', '');
|
|
|
|
expect(test).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|