2017-10-25 04:00:07 +00:00
|
|
|
const defaultConfig = require('../../lib/config/defaults').getConfig();
|
|
|
|
const manager = require('../../lib/manager');
|
2018-03-03 13:39:39 +00:00
|
|
|
const npm = require('../../lib/manager/npm');
|
|
|
|
const meteor = require('../../lib/manager/meteor');
|
|
|
|
const docker = require('../../lib/manager/docker');
|
2018-03-03 14:28:46 +00:00
|
|
|
const node = require('../../lib/manager/travis');
|
2018-03-03 13:39:39 +00:00
|
|
|
const bazel = require('../../lib/manager/bazel');
|
2017-12-07 08:22:10 +00:00
|
|
|
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs-extra');
|
2017-10-25 04:48:08 +00:00
|
|
|
|
|
|
|
const { getUpdatedPackageFiles } = manager;
|
|
|
|
|
2017-10-25 04:00:07 +00:00
|
|
|
describe('manager', () => {
|
|
|
|
describe('detectPackageFiles(config)', () => {
|
|
|
|
let config;
|
|
|
|
beforeEach(() => {
|
|
|
|
config = {
|
2018-04-03 04:52:04 +00:00
|
|
|
...JSON.parse(JSON.stringify(defaultConfig)),
|
2017-10-25 04:00:07 +00:00
|
|
|
warnings: [],
|
|
|
|
};
|
|
|
|
});
|
2018-03-30 15:00:51 +00:00
|
|
|
it('skips if not in enabledManagers list', async () => {
|
|
|
|
platform.getFileList.mockReturnValueOnce([
|
|
|
|
'package.json',
|
|
|
|
'backend/package.json',
|
|
|
|
]);
|
|
|
|
config.enabledManagers = ['docker'];
|
|
|
|
const res = await manager.detectPackageFiles(config);
|
|
|
|
expect(res).toHaveLength(0);
|
|
|
|
});
|
2018-04-30 05:16:34 +00:00
|
|
|
it('skips if language is disabled', async () => {
|
2018-04-03 04:52:04 +00:00
|
|
|
platform.getFileList.mockReturnValueOnce([
|
|
|
|
'package.json',
|
|
|
|
'.circleci/config.yml',
|
|
|
|
]);
|
|
|
|
config.docker.enabled = false;
|
|
|
|
const res = await manager.detectPackageFiles(config);
|
|
|
|
expect(res).toHaveLength(1);
|
|
|
|
});
|
2017-10-25 04:00:07 +00:00
|
|
|
it('adds package files to object', async () => {
|
2017-11-07 10:46:10 +00:00
|
|
|
platform.getFileList.mockReturnValueOnce([
|
2017-10-25 04:00:07 +00:00
|
|
|
'package.json',
|
|
|
|
'backend/package.json',
|
|
|
|
]);
|
|
|
|
const res = await manager.detectPackageFiles(config);
|
2017-11-03 08:25:18 +00:00
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
expect(res).toHaveLength(2);
|
2017-10-25 04:00:07 +00:00
|
|
|
});
|
|
|
|
it('finds meteor package files', async () => {
|
|
|
|
config.meteor.enabled = true;
|
2017-11-07 10:46:10 +00:00
|
|
|
platform.getFileList.mockReturnValueOnce([
|
2017-10-25 04:00:07 +00:00
|
|
|
'modules/something/package.js',
|
|
|
|
]); // meteor
|
2017-11-08 11:23:32 +00:00
|
|
|
platform.getFile.mockReturnValueOnce('Npm.depends( {} )');
|
2017-10-25 04:00:07 +00:00
|
|
|
const res = await manager.detectPackageFiles(config);
|
2017-11-03 08:25:18 +00:00
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
expect(res).toHaveLength(1);
|
2017-10-25 04:00:07 +00:00
|
|
|
});
|
|
|
|
it('skips meteor package files with no json', async () => {
|
|
|
|
config.meteor.enabled = true;
|
2017-11-07 10:46:10 +00:00
|
|
|
platform.getFileList.mockReturnValueOnce([
|
2017-10-25 04:00:07 +00:00
|
|
|
'modules/something/package.js',
|
|
|
|
]); // meteor
|
2017-11-08 11:23:32 +00:00
|
|
|
platform.getFile.mockReturnValueOnce('Npm.depends(packages)');
|
2017-10-25 04:00:07 +00:00
|
|
|
const res = await manager.detectPackageFiles(config);
|
2017-11-03 08:25:18 +00:00
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
expect(res).toHaveLength(0);
|
2017-10-25 04:00:07 +00:00
|
|
|
});
|
|
|
|
it('finds Dockerfiles', async () => {
|
2017-11-07 10:46:10 +00:00
|
|
|
platform.getFileList.mockReturnValueOnce([
|
2017-10-25 04:00:07 +00:00
|
|
|
'Dockerfile',
|
|
|
|
'other/Dockerfile',
|
2018-01-19 05:59:35 +00:00
|
|
|
'another/Dockerfile',
|
2017-10-25 04:00:07 +00:00
|
|
|
]);
|
2017-11-08 11:23:32 +00:00
|
|
|
platform.getFile.mockReturnValueOnce(
|
2018-01-19 05:59:35 +00:00
|
|
|
'### comment\n\n \nFROM something\nRUN something\nFROM something-else\nRUN bar'
|
2017-10-25 04:00:07 +00:00
|
|
|
);
|
2017-11-08 11:23:32 +00:00
|
|
|
platform.getFile.mockReturnValueOnce(
|
2017-10-25 04:00:07 +00:00
|
|
|
'ARG foo\nFROM something\nRUN something'
|
|
|
|
);
|
2018-01-19 05:59:35 +00:00
|
|
|
platform.getFile.mockReturnValueOnce(
|
|
|
|
'ARG foo\nno FROM at all\nRUN something'
|
|
|
|
);
|
2017-10-25 04:00:07 +00:00
|
|
|
const res = await manager.detectPackageFiles(config);
|
2017-11-03 08:25:18 +00:00
|
|
|
expect(res).toMatchSnapshot();
|
2018-01-19 05:59:35 +00:00
|
|
|
expect(res).toHaveLength(2);
|
2017-10-25 04:00:07 +00:00
|
|
|
});
|
2017-12-05 06:50:16 +00:00
|
|
|
it('finds .travis.yml files', async () => {
|
2018-03-04 06:08:19 +00:00
|
|
|
config.travis.enabled = true;
|
2017-12-05 06:50:16 +00:00
|
|
|
platform.getFileList.mockReturnValueOnce([
|
|
|
|
'.travis.yml',
|
|
|
|
'other/.travis.yml',
|
|
|
|
]);
|
2018-03-05 12:50:54 +00:00
|
|
|
platform.getFile.mockReturnValueOnce('sudo: true\nnode_js:\n -8\n');
|
2017-12-05 06:50:16 +00:00
|
|
|
const res = await manager.detectPackageFiles(config);
|
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
expect(res).toHaveLength(1);
|
|
|
|
});
|
2018-03-04 14:52:06 +00:00
|
|
|
it('finds .nvmrc files', async () => {
|
|
|
|
config.travis.enabled = true;
|
|
|
|
platform.getFileList.mockReturnValueOnce(['.nvmrc', 'other/.nvmrc']);
|
|
|
|
const res = await manager.detectPackageFiles(config);
|
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
expect(res).toHaveLength(1);
|
|
|
|
});
|
2017-12-07 08:22:10 +00:00
|
|
|
it('finds WORKSPACE files', async () => {
|
|
|
|
config.bazel.enabled = true;
|
|
|
|
platform.getFileList.mockReturnValueOnce([
|
|
|
|
'WORKSPACE',
|
|
|
|
'other/WORKSPACE',
|
2018-03-04 19:03:52 +00:00
|
|
|
'empty/WORKSPACE',
|
2017-12-07 08:22:10 +00:00
|
|
|
]);
|
|
|
|
platform.getFile.mockReturnValueOnce('\n\ngit_repository(\n\n)\n');
|
|
|
|
platform.getFile.mockReturnValueOnce(
|
|
|
|
await fs.readFile(
|
|
|
|
path.resolve('test/_fixtures/bazel/WORKSPACE1'),
|
|
|
|
'utf8'
|
|
|
|
)
|
|
|
|
);
|
2018-03-04 19:03:52 +00:00
|
|
|
platform.getFile.mockReturnValueOnce('foo');
|
2017-12-07 08:22:10 +00:00
|
|
|
const res = await manager.detectPackageFiles(config);
|
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
expect(res).toHaveLength(2);
|
|
|
|
});
|
2017-11-05 13:23:20 +00:00
|
|
|
it('skips Dockerfiles with no content', async () => {
|
2017-11-07 10:46:10 +00:00
|
|
|
platform.getFileList.mockReturnValueOnce(['Dockerfile']);
|
2017-11-08 11:23:32 +00:00
|
|
|
platform.getFile.mockReturnValueOnce(null);
|
2017-11-05 13:23:20 +00:00
|
|
|
const res = await manager.detectPackageFiles(config);
|
|
|
|
expect(res).toHaveLength(0);
|
|
|
|
});
|
2017-10-25 04:00:07 +00:00
|
|
|
it('ignores node modules', async () => {
|
2017-11-07 10:46:10 +00:00
|
|
|
platform.getFileList.mockReturnValueOnce([
|
2017-10-25 04:00:07 +00:00
|
|
|
'package.json',
|
|
|
|
'node_modules/backend/package.json',
|
|
|
|
]);
|
|
|
|
const res = await manager.detectPackageFiles(config);
|
2017-11-03 08:25:18 +00:00
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
expect(res).toHaveLength(1);
|
2017-10-25 04:00:07 +00:00
|
|
|
expect(res.foundIgnoredPaths).toMatchSnapshot();
|
|
|
|
expect(res.warnings).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2018-03-04 15:09:12 +00:00
|
|
|
describe('getManager', () => {
|
|
|
|
it('rejects unknown files', () => {
|
2018-04-30 11:18:51 +00:00
|
|
|
expect(manager.getManager(defaultConfig, 'WORKSPACER')).toBe(null);
|
2018-03-04 15:09:12 +00:00
|
|
|
});
|
|
|
|
it('detects files in root', () => {
|
2018-04-30 11:18:51 +00:00
|
|
|
expect(manager.getManager(defaultConfig, 'WORKSPACE')).toBe('bazel');
|
|
|
|
expect(manager.getManager(defaultConfig, 'Dockerfile')).toBe('docker');
|
|
|
|
expect(manager.getManager(defaultConfig, 'package.js')).toBe('meteor');
|
|
|
|
expect(manager.getManager(defaultConfig, 'package.json')).toBe('npm');
|
|
|
|
expect(manager.getManager(defaultConfig, '.nvmrc')).toBe('nvm');
|
|
|
|
expect(manager.getManager(defaultConfig, '.travis.yml')).toBe('travis');
|
2018-03-04 15:09:12 +00:00
|
|
|
});
|
|
|
|
it('detects nested files', () => {
|
2018-04-30 11:18:51 +00:00
|
|
|
expect(manager.getManager(defaultConfig, 'foo/bar/WORKSPACE')).toBe(
|
|
|
|
'bazel'
|
|
|
|
);
|
|
|
|
expect(manager.getManager(defaultConfig, 'backend/Dockerfile')).toBe(
|
|
|
|
'docker'
|
|
|
|
);
|
|
|
|
expect(manager.getManager(defaultConfig, 'package/a/package.js')).toBe(
|
|
|
|
'meteor'
|
|
|
|
);
|
|
|
|
expect(manager.getManager(defaultConfig, 'frontend/package.json')).toBe(
|
|
|
|
'npm'
|
|
|
|
);
|
|
|
|
expect(manager.getManager(defaultConfig, 'subfolder-1/.nvmrc')).toBe(
|
|
|
|
null
|
|
|
|
);
|
|
|
|
expect(manager.getManager(defaultConfig, 'subfolder-2/.travis.yml')).toBe(
|
|
|
|
null
|
|
|
|
);
|
2018-03-04 15:09:12 +00:00
|
|
|
});
|
|
|
|
});
|
2017-10-25 04:48:08 +00:00
|
|
|
describe('getUpdatedPackageFiles', () => {
|
|
|
|
let config;
|
|
|
|
beforeEach(() => {
|
|
|
|
config = {
|
|
|
|
...defaultConfig,
|
|
|
|
parentBranch: 'some-branch',
|
|
|
|
};
|
2018-03-30 04:05:00 +00:00
|
|
|
npm.updateDependency = jest.fn();
|
|
|
|
docker.updateDependency = jest.fn();
|
|
|
|
meteor.updateDependency = jest.fn();
|
|
|
|
node.updateDependency = jest.fn();
|
|
|
|
bazel.updateDependency = jest.fn();
|
2017-10-25 04:48:08 +00:00
|
|
|
});
|
|
|
|
it('returns empty if lock file maintenance', async () => {
|
|
|
|
config.upgrades = [{ type: 'lockFileMaintenance' }];
|
|
|
|
const res = await getUpdatedPackageFiles(config);
|
|
|
|
expect(res.updatedPackageFiles).toHaveLength(0);
|
|
|
|
});
|
2018-03-30 04:05:00 +00:00
|
|
|
it('recurses if updateDependency error', async () => {
|
2017-10-25 04:48:08 +00:00
|
|
|
config.parentBranch = 'some-branch';
|
|
|
|
config.canRebase = true;
|
2018-02-28 18:01:35 +00:00
|
|
|
config.upgrades = [{ packageFile: 'package.json', manager: 'npm' }];
|
2018-03-30 04:05:00 +00:00
|
|
|
npm.updateDependency.mockReturnValueOnce(null);
|
|
|
|
npm.updateDependency.mockReturnValueOnce('some content');
|
2017-10-25 04:48:08 +00:00
|
|
|
const res = await getUpdatedPackageFiles(config);
|
|
|
|
expect(res.updatedPackageFiles).toHaveLength(1);
|
|
|
|
});
|
|
|
|
it('errors if cannot rebase', async () => {
|
2018-03-03 13:39:39 +00:00
|
|
|
config.upgrades = [{ packageFile: 'package.json', manager: 'npm' }];
|
2017-10-25 04:48:08 +00:00
|
|
|
let e;
|
|
|
|
try {
|
|
|
|
await getUpdatedPackageFiles(config);
|
|
|
|
} catch (err) {
|
|
|
|
e = err;
|
|
|
|
}
|
|
|
|
expect(e).toBeDefined();
|
|
|
|
});
|
|
|
|
it('returns updated files', async () => {
|
|
|
|
config.parentBranch = 'some-branch';
|
|
|
|
config.canRebase = true;
|
|
|
|
config.upgrades = [
|
2018-02-28 18:01:35 +00:00
|
|
|
{ packageFile: 'package.json', manager: 'npm' },
|
|
|
|
{ packageFile: 'Dockerfile', manager: 'docker' },
|
|
|
|
{ packageFile: 'packages/foo/package.js', manager: 'meteor' },
|
2018-03-03 14:28:46 +00:00
|
|
|
{ packageFile: '.travis.yml', manager: 'travis' },
|
2018-02-28 18:01:35 +00:00
|
|
|
{ packageFile: 'WORKSPACE', manager: 'bazel' },
|
2017-10-25 04:48:08 +00:00
|
|
|
];
|
2017-11-08 11:23:32 +00:00
|
|
|
platform.getFile.mockReturnValueOnce('old content 1');
|
|
|
|
platform.getFile.mockReturnValueOnce('old content 1');
|
|
|
|
platform.getFile.mockReturnValueOnce('old content 2');
|
|
|
|
platform.getFile.mockReturnValueOnce('old content 3');
|
2017-12-05 06:50:16 +00:00
|
|
|
platform.getFile.mockReturnValueOnce('old travis');
|
2017-12-07 08:22:10 +00:00
|
|
|
platform.getFile.mockReturnValueOnce('old WORKSPACE');
|
2018-03-30 04:05:00 +00:00
|
|
|
npm.updateDependency.mockReturnValueOnce('new content 1');
|
|
|
|
npm.updateDependency.mockReturnValueOnce('new content 1+');
|
|
|
|
docker.updateDependency.mockReturnValueOnce('new content 2');
|
|
|
|
meteor.updateDependency.mockReturnValueOnce('old content 3');
|
|
|
|
node.updateDependency.mockReturnValueOnce('old travis');
|
|
|
|
bazel.updateDependency.mockReturnValueOnce('old WORKSPACE');
|
2017-10-25 04:48:08 +00:00
|
|
|
const res = await getUpdatedPackageFiles(config);
|
|
|
|
expect(res.updatedPackageFiles).toHaveLength(2);
|
|
|
|
});
|
|
|
|
});
|
2017-10-25 04:00:07 +00:00
|
|
|
});
|