2021-04-23 16:58:48 +00:00
|
|
|
import { getName, loadFixture } from '../../../test/util';
|
2021-02-05 11:30:01 +00:00
|
|
|
import { setAdminConfig } from '../../config/admin';
|
2020-02-05 00:14:31 +00:00
|
|
|
import { extractPackageFile } from './extract';
|
2018-04-28 18:39:07 +00:00
|
|
|
|
2021-04-23 16:58:48 +00:00
|
|
|
const requirements1 = loadFixture(__filename, 'requirements1.txt');
|
|
|
|
const requirements2 = loadFixture(__filename, 'requirements2.txt');
|
|
|
|
const requirements3 = loadFixture(__filename, 'requirements3.txt');
|
|
|
|
const requirements4 = loadFixture(__filename, 'requirements4.txt');
|
|
|
|
const requirements5 = loadFixture(__filename, 'requirements5.txt');
|
|
|
|
const requirements6 = loadFixture(__filename, 'requirements6.txt');
|
|
|
|
const requirements7 = loadFixture(__filename, 'requirements7.txt');
|
2020-07-07 04:28:50 +00:00
|
|
|
|
2021-04-13 10:07:57 +00:00
|
|
|
describe(getName(__filename), () => {
|
2020-07-07 04:28:50 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
delete process.env.PIP_TEST_TOKEN;
|
2021-02-05 11:30:01 +00:00
|
|
|
setAdminConfig();
|
2020-07-07 04:28:50 +00:00
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
delete process.env.PIP_TEST_TOKEN;
|
2021-02-05 11:30:01 +00:00
|
|
|
setAdminConfig();
|
2020-07-07 04:28:50 +00:00
|
|
|
});
|
2018-11-04 17:51:23 +00:00
|
|
|
describe('extractPackageFile()', () => {
|
2018-04-28 18:39:07 +00:00
|
|
|
let config;
|
2021-01-20 09:07:26 +00:00
|
|
|
const OLD_ENV = process.env;
|
2018-04-28 18:39:07 +00:00
|
|
|
beforeEach(() => {
|
2019-07-15 20:36:09 +00:00
|
|
|
config = { registryUrls: ['AnExistingDefaultUrl'] };
|
2021-01-20 09:07:26 +00:00
|
|
|
process.env = { ...OLD_ENV };
|
|
|
|
delete process.env.PIP_INDEX_URL;
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
process.env = OLD_ENV;
|
2018-04-28 18:39:07 +00:00
|
|
|
});
|
2018-05-03 16:09:18 +00:00
|
|
|
it('returns null for empty', () => {
|
2019-07-15 20:36:09 +00:00
|
|
|
expect(
|
|
|
|
extractPackageFile('nothing here', 'requirements.txt', config)
|
|
|
|
).toBeNull();
|
2018-05-03 16:09:18 +00:00
|
|
|
});
|
2018-04-28 18:39:07 +00:00
|
|
|
it('extracts dependencies', () => {
|
2019-07-15 20:36:09 +00:00
|
|
|
const res = extractPackageFile(requirements1, 'unused_file_name', config);
|
2018-06-05 09:26:08 +00:00
|
|
|
expect(res).toMatchSnapshot();
|
2019-06-20 08:20:27 +00:00
|
|
|
expect(res.registryUrls).toEqual(['http://example.com/private-pypi/']);
|
|
|
|
expect(res.deps).toHaveLength(3);
|
2018-06-05 09:26:08 +00:00
|
|
|
});
|
|
|
|
it('extracts multiple dependencies', () => {
|
2019-07-15 20:36:09 +00:00
|
|
|
const res = extractPackageFile(requirements2, 'unused_file_name', config)
|
|
|
|
.deps;
|
2018-04-28 18:39:07 +00:00
|
|
|
expect(res).toMatchSnapshot();
|
2018-06-05 09:26:08 +00:00
|
|
|
expect(res).toHaveLength(5);
|
2018-04-28 18:39:07 +00:00
|
|
|
});
|
2018-10-23 04:09:33 +00:00
|
|
|
it('handles comments and commands', () => {
|
2019-07-15 20:36:09 +00:00
|
|
|
const res = extractPackageFile(requirements3, 'unused_file_name', config)
|
|
|
|
.deps;
|
2018-10-23 04:09:33 +00:00
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
expect(res).toHaveLength(5);
|
|
|
|
});
|
2019-06-20 08:25:17 +00:00
|
|
|
it('handles extras and complex index url', () => {
|
2019-07-15 20:36:09 +00:00
|
|
|
const res = extractPackageFile(requirements4, 'unused_file_name', config);
|
2018-11-01 10:46:00 +00:00
|
|
|
expect(res).toMatchSnapshot();
|
2019-06-20 08:25:17 +00:00
|
|
|
expect(res.registryUrls).toEqual([
|
|
|
|
'https://artifactory.company.com/artifactory/api/pypi/python/simple',
|
|
|
|
]);
|
|
|
|
expect(res.deps).toHaveLength(3);
|
2018-11-01 10:46:00 +00:00
|
|
|
});
|
2019-07-15 20:36:09 +00:00
|
|
|
it('handles extra index url', () => {
|
|
|
|
const res = extractPackageFile(requirements5, 'unused_file_name', config);
|
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
expect(res.registryUrls).toEqual([
|
|
|
|
'https://artifactory.company.com/artifactory/api/pypi/python/simple',
|
|
|
|
'http://example.com/private-pypi/',
|
|
|
|
]);
|
|
|
|
expect(res.deps).toHaveLength(6);
|
|
|
|
});
|
|
|
|
it('handles extra index url and defaults without index to config', () => {
|
|
|
|
const res = extractPackageFile(requirements6, 'unused_file_name', config);
|
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
expect(res.registryUrls).toEqual([
|
|
|
|
'AnExistingDefaultUrl',
|
|
|
|
'http://example.com/private-pypi/',
|
|
|
|
]);
|
|
|
|
expect(res.deps).toHaveLength(6);
|
|
|
|
});
|
|
|
|
it('handles extra index url and defaults without index to pypi', () => {
|
|
|
|
const res = extractPackageFile(requirements6, 'unused_file_name', {});
|
|
|
|
expect(res).toMatchSnapshot();
|
|
|
|
expect(res.registryUrls).toEqual([
|
|
|
|
'https://pypi.org/pypi/',
|
|
|
|
'http://example.com/private-pypi/',
|
|
|
|
]);
|
|
|
|
expect(res.deps).toHaveLength(6);
|
|
|
|
});
|
2020-07-07 04:28:50 +00:00
|
|
|
it('should not replace env vars in low trust mode', () => {
|
|
|
|
process.env.PIP_TEST_TOKEN = 'its-a-secret';
|
|
|
|
const res = extractPackageFile(requirements7, 'unused_file_name', {});
|
|
|
|
expect(res.registryUrls).toEqual([
|
|
|
|
'https://pypi.org/pypi/',
|
|
|
|
'http://$PIP_TEST_TOKEN:example.com/private-pypi/',
|
|
|
|
// eslint-disable-next-line no-template-curly-in-string
|
|
|
|
'http://${PIP_TEST_TOKEN}:example.com/private-pypi/',
|
|
|
|
'http://$PIP_TEST_TOKEN:example.com/private-pypi/',
|
|
|
|
// eslint-disable-next-line no-template-curly-in-string
|
|
|
|
'http://${PIP_TEST_TOKEN}:example.com/private-pypi/',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it('should replace env vars in high trust mode', () => {
|
|
|
|
process.env.PIP_TEST_TOKEN = 'its-a-secret';
|
2021-04-15 20:15:30 +00:00
|
|
|
setAdminConfig({ exposeAllEnv: true });
|
2020-07-07 04:28:50 +00:00
|
|
|
const res = extractPackageFile(requirements7, 'unused_file_name', {});
|
|
|
|
expect(res.registryUrls).toEqual([
|
|
|
|
'https://pypi.org/pypi/',
|
|
|
|
'http://its-a-secret:example.com/private-pypi/',
|
|
|
|
'http://its-a-secret:example.com/private-pypi/',
|
|
|
|
'http://its-a-secret:example.com/private-pypi/',
|
|
|
|
'http://its-a-secret:example.com/private-pypi/',
|
|
|
|
]);
|
|
|
|
});
|
2018-04-28 18:39:07 +00:00
|
|
|
});
|
|
|
|
});
|