2021-10-26 04:27:18 +00:00
|
|
|
import { DirectoryResult, dir } from 'tmp-promise';
|
2021-01-17 08:18:32 +00:00
|
|
|
import { join } from 'upath';
|
2022-07-04 04:18:53 +00:00
|
|
|
import { Fixtures } from '../../../../test/fixtures';
|
2022-03-03 09:35:26 +00:00
|
|
|
import { GlobalConfig } from '../../../config/global';
|
|
|
|
import type { RepoGlobalConfig } from '../../../config/types';
|
|
|
|
import { writeLocalFile } from '../../../util/fs';
|
2021-05-17 13:21:28 +00:00
|
|
|
import type { ExtractConfig } from '../types';
|
2022-06-20 15:05:39 +00:00
|
|
|
import { extractPackageFile } from '.';
|
2018-11-09 13:22:11 +00:00
|
|
|
|
2022-07-04 04:18:53 +00:00
|
|
|
const cargo1toml = Fixtures.get('Cargo.1.toml');
|
|
|
|
const cargo2toml = Fixtures.get('Cargo.2.toml');
|
|
|
|
const cargo3toml = Fixtures.get('Cargo.3.toml');
|
|
|
|
const cargo4toml = Fixtures.get('Cargo.4.toml');
|
|
|
|
const cargo5toml = Fixtures.get('Cargo.5.toml');
|
|
|
|
const cargo6configtoml = Fixtures.get('cargo.6.config.toml');
|
|
|
|
const cargo6toml = Fixtures.get('Cargo.6.toml');
|
2021-01-17 08:18:32 +00:00
|
|
|
|
2022-03-03 09:35:26 +00:00
|
|
|
describe('modules/manager/cargo/extract', () => {
|
2018-11-09 13:22:11 +00:00
|
|
|
describe('extractPackageFile()', () => {
|
2021-05-17 13:21:28 +00:00
|
|
|
let config: ExtractConfig;
|
2021-08-16 16:00:51 +00:00
|
|
|
let adminConfig: RepoGlobalConfig;
|
2021-10-26 04:27:18 +00:00
|
|
|
let tmpDir: DirectoryResult;
|
2021-05-17 13:21:28 +00:00
|
|
|
|
|
|
|
beforeEach(async () => {
|
2018-11-09 13:22:11 +00:00
|
|
|
config = {};
|
2021-10-26 04:27:18 +00:00
|
|
|
tmpDir = await dir({ unsafeCleanup: true });
|
2021-05-17 13:21:28 +00:00
|
|
|
adminConfig = {
|
2021-05-26 11:22:16 +00:00
|
|
|
localDir: join(tmpDir.path, 'local'),
|
2021-05-17 13:21:28 +00:00
|
|
|
cacheDir: join(tmpDir.path, 'cache'),
|
|
|
|
};
|
|
|
|
|
2021-11-23 20:10:45 +00:00
|
|
|
GlobalConfig.set(adminConfig);
|
2021-05-17 13:21:28 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-10-26 04:27:18 +00:00
|
|
|
afterEach(async () => {
|
|
|
|
await tmpDir.cleanup();
|
2021-11-23 20:10:45 +00:00
|
|
|
GlobalConfig.reset();
|
2018-11-09 13:22:11 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('returns null for invalid toml', async () => {
|
|
|
|
expect(
|
|
|
|
await extractPackageFile('invalid toml', 'Cargo.toml', config)
|
|
|
|
).toBeNull();
|
2019-04-09 08:25:13 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('returns null for empty dependencies', async () => {
|
2019-04-09 08:25:13 +00:00
|
|
|
const cargotoml = '[dependencies]\n';
|
2021-01-17 08:18:32 +00:00
|
|
|
expect(
|
|
|
|
await extractPackageFile(cargotoml, 'Cargo.toml', config)
|
|
|
|
).toBeNull();
|
2019-04-09 08:25:13 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('returns null for empty dev-dependencies', async () => {
|
2019-04-09 08:25:13 +00:00
|
|
|
const cargotoml = '[dev-dependencies]\n';
|
2021-01-17 08:18:32 +00:00
|
|
|
expect(
|
|
|
|
await extractPackageFile(cargotoml, 'Cargo.toml', config)
|
|
|
|
).toBeNull();
|
2019-04-09 08:25:13 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('returns null for empty custom target', async () => {
|
2019-04-09 08:25:13 +00:00
|
|
|
const cargotoml = '[target."foo".dependencies]\n';
|
2021-01-17 08:18:32 +00:00
|
|
|
expect(
|
|
|
|
await extractPackageFile(cargotoml, 'Cargo.toml', config)
|
|
|
|
).toBeNull();
|
2018-11-09 13:22:11 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('extracts multiple dependencies simple', async () => {
|
|
|
|
const res = await extractPackageFile(cargo1toml, 'Cargo.toml', config);
|
2022-06-20 15:05:39 +00:00
|
|
|
expect(res?.deps).toMatchSnapshot();
|
|
|
|
expect(res?.deps).toHaveLength(15);
|
2019-03-04 09:24:07 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('extracts multiple dependencies advanced', async () => {
|
|
|
|
const res = await extractPackageFile(cargo2toml, 'Cargo.toml', config);
|
2022-06-20 15:05:39 +00:00
|
|
|
expect(res?.deps).toMatchSnapshot();
|
|
|
|
expect(res?.deps).toHaveLength(18 + 6 + 1);
|
2019-03-04 09:24:07 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('handles inline tables', async () => {
|
|
|
|
const res = await extractPackageFile(cargo3toml, 'Cargo.toml', config);
|
2022-06-20 15:05:39 +00:00
|
|
|
expect(res?.deps).toMatchSnapshot();
|
|
|
|
expect(res?.deps).toHaveLength(8);
|
2019-03-04 09:24:07 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('handles standard tables', async () => {
|
|
|
|
const res = await extractPackageFile(cargo4toml, 'Cargo.toml', config);
|
2022-06-20 15:05:39 +00:00
|
|
|
expect(res?.deps).toMatchSnapshot();
|
|
|
|
expect(res?.deps).toHaveLength(6);
|
2019-04-09 08:25:13 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('extracts platform specific dependencies', async () => {
|
|
|
|
const res = await extractPackageFile(cargo5toml, 'Cargo.toml', config);
|
2022-06-20 15:05:39 +00:00
|
|
|
expect(res?.deps).toMatchSnapshot();
|
|
|
|
expect(res?.deps).toHaveLength(4);
|
2019-03-04 09:24:07 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('extracts registry urls from .cargo/config.toml', async () => {
|
|
|
|
await writeLocalFile('.cargo/config.toml', cargo6configtoml);
|
|
|
|
const res = await extractPackageFile(cargo6toml, 'Cargo.toml', {
|
|
|
|
...config,
|
|
|
|
});
|
2022-06-20 15:05:39 +00:00
|
|
|
expect(res?.deps).toMatchSnapshot();
|
|
|
|
expect(res?.deps).toHaveLength(3);
|
2021-01-17 08:18:32 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('extracts registry urls from .cargo/config (legacy path)', async () => {
|
|
|
|
await writeLocalFile('.cargo/config', cargo6configtoml);
|
|
|
|
const res = await extractPackageFile(cargo6toml, 'Cargo.toml', {
|
|
|
|
...config,
|
|
|
|
});
|
2022-06-20 15:05:39 +00:00
|
|
|
expect(res?.deps).toMatchSnapshot();
|
|
|
|
expect(res?.deps).toHaveLength(3);
|
2021-01-17 08:18:32 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('skips unknown registries', async () => {
|
|
|
|
const cargotoml =
|
|
|
|
'[dependencies]\nfoobar = { version = "0.1.0", registry = "not-listed" }';
|
|
|
|
const res = await extractPackageFile(cargotoml, 'Cargo.toml', config);
|
2022-06-20 15:05:39 +00:00
|
|
|
expect(res?.deps).toMatchSnapshot();
|
|
|
|
expect(res?.deps).toHaveLength(1);
|
2021-01-17 08:18:32 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('fails to parse cargo config with invalid TOML', async () => {
|
|
|
|
await writeLocalFile('.cargo/config', '[registries');
|
|
|
|
|
|
|
|
const res = await extractPackageFile(cargo6toml, 'Cargo.toml', {
|
|
|
|
...config,
|
|
|
|
});
|
2022-06-20 15:05:39 +00:00
|
|
|
expect(res?.deps).toMatchSnapshot();
|
|
|
|
expect(res?.deps).toHaveLength(3);
|
2021-01-17 08:18:32 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-01-17 08:18:32 +00:00
|
|
|
it('ignore cargo config registries with missing index', async () => {
|
|
|
|
await writeLocalFile('.cargo/config', '[registries.mine]\nfoo = "bar"');
|
|
|
|
|
|
|
|
const res = await extractPackageFile(cargo6toml, 'Cargo.toml', {
|
|
|
|
...config,
|
|
|
|
});
|
2022-06-20 15:05:39 +00:00
|
|
|
expect(res?.deps).toMatchSnapshot();
|
|
|
|
expect(res?.deps).toHaveLength(3);
|
2021-01-17 08:18:32 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2021-06-11 16:41:46 +00:00
|
|
|
it('extracts original package name of renamed dependencies', async () => {
|
|
|
|
const cargotoml =
|
|
|
|
'[dependencies]\nboolector-solver = { package = "boolector", version = "0.4.0" }';
|
|
|
|
const res = await extractPackageFile(cargotoml, 'Cargo.toml', config);
|
|
|
|
|
2022-06-20 15:05:39 +00:00
|
|
|
expect(res?.deps).toMatchSnapshot();
|
|
|
|
expect(res?.deps).toHaveLength(1);
|
|
|
|
expect(res?.deps[0].packageName).toBe('boolector');
|
2021-06-11 16:41:46 +00:00
|
|
|
});
|
2018-11-09 13:22:11 +00:00
|
|
|
});
|
|
|
|
});
|