refactor(cargo): use autoUpdate

This commit is contained in:
Rhys Arkins 2020-04-18 20:26:25 +02:00
parent 74d77db65d
commit 0ab9de4293
4 changed files with 1 additions and 609 deletions

View file

@ -1,241 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`lib/manager/cargo/update updateDependency() updates nested version dependency 1`] = `
"[package]
name = \\"rustcap\\"
version = \\"0.1.2\\"
description = \\"Wrapper for libpcap\\"
homepage = \\"https://github.com/jmmk/rustcap\\"
repository = \\"https://github.com/jmmk/rustcap\\"
authors = [\\"Michael McLellan <jmikem825@gmail.com>\\"]
keywords = [\\"pcap\\", \\"libpcap\\"]
license = \\"MIT\\"
readme = \\"README.md\\"
documentation = \\"https://docs.rs/rustcap\\"
[workspace]
members = [\\"pcap-sys\\"]
[dependencies]
libc = \\"=0.2.43\\"
bitflags = \\"1.0.4\\"
pcap-sys = { version = \\"0.2.0\\", path = \\"pcap-sys\\" }
pnet = { version = \\"0.21.0\\", optional = true, default-features = false}
git_dep_with_version = { version = \\"0.1.0\\", git = \\"https://github.com/foo/bar\\" }
git_dep = { git = \\"https://github.com/foo/bar\\" }
same_version_1__ = \\"0.0.0\\"
same_version_1_ = \\"0.0.0\\"
same_version_1 = \\"0.0.0\\"
[target.'cfg(windows)'.dependencies.winapi]
version = \\"=0.3.6\\"
features = [\\"ws2def\\", \\"ws2ipdef\\"]
[target.'cfg(target_arch = \\"wasm32\\")'.dependencies]
wasm-bindgen = \\"0.2.37\\"
js-sys = \\"0.3.14\\"
js_relative_import = { path = \\"../../common/js_relative_import\\" }
[target.'cfg(target_arch = \\"wasm32\\")'.dependencies.web-sys]
version = \\"0.3.14\\"
features = [
\\"AudioBuffer\\",
\\"AudioBufferSourceNode\\",
\\"AudioContext\\",
\\"AudioDestinationNode\\",
\\"AudioNode\\",
]
[dependencies.dep1]
version = \\"0.4.2\\"
[features]
libpnet = [\\"pnet\\"]
[dev-dependencies]
# libc = \\"0.1.1\\"
# pnet = { version = \\"0.19.0\\", optional = true, default-features = false}
# [target.'cfg(windows)'.dependencies.winapi]
# version = \\"0.3.0\\"
# features = [\\"ws2def\\", \\"ws2ipdef\\"]"
`;
exports[`lib/manager/cargo/update updateDependency() updates normal dependency 1`] = `
"[package]
name = \\"rustcap\\"
version = \\"0.1.2\\"
description = \\"Wrapper for libpcap\\"
homepage = \\"https://github.com/jmmk/rustcap\\"
repository = \\"https://github.com/jmmk/rustcap\\"
authors = [\\"Michael McLellan <jmikem825@gmail.com>\\"]
keywords = [\\"pcap\\", \\"libpcap\\"]
license = \\"MIT\\"
readme = \\"README.md\\"
documentation = \\"https://docs.rs/rustcap\\"
[workspace]
members = [\\"pcap-sys\\"]
[dependencies]
libc = \\"0.3.0\\"
bitflags = \\"1.0.4\\"
pcap-sys = { version = \\"=0.1\\", path = \\"pcap-sys\\" }
pnet = { version = \\"0.21.0\\", optional = true, default-features = false}
git_dep_with_version = { version = \\"0.1.0\\", git = \\"https://github.com/foo/bar\\" }
git_dep = { git = \\"https://github.com/foo/bar\\" }
same_version_1__ = \\"0.0.0\\"
same_version_1_ = \\"0.0.0\\"
same_version_1 = \\"0.0.0\\"
[target.'cfg(windows)'.dependencies.winapi]
version = \\"=0.3.6\\"
features = [\\"ws2def\\", \\"ws2ipdef\\"]
[target.'cfg(target_arch = \\"wasm32\\")'.dependencies]
wasm-bindgen = \\"0.2.37\\"
js-sys = \\"0.3.14\\"
js_relative_import = { path = \\"../../common/js_relative_import\\" }
[target.'cfg(target_arch = \\"wasm32\\")'.dependencies.web-sys]
version = \\"0.3.14\\"
features = [
\\"AudioBuffer\\",
\\"AudioBufferSourceNode\\",
\\"AudioContext\\",
\\"AudioDestinationNode\\",
\\"AudioNode\\",
]
[dependencies.dep1]
version = \\"0.4.2\\"
[features]
libpnet = [\\"pnet\\"]
[dev-dependencies]
# libc = \\"0.1.1\\"
# pnet = { version = \\"0.19.0\\", optional = true, default-features = false}
# [target.'cfg(windows)'.dependencies.winapi]
# version = \\"0.3.0\\"
# features = [\\"ws2def\\", \\"ws2ipdef\\"]"
`;
exports[`lib/manager/cargo/update updateDependency() updates normal dependency with mismatch on first try 1`] = `
"[package]
name = \\"rustcap\\"
version = \\"0.1.2\\"
description = \\"Wrapper for libpcap\\"
homepage = \\"https://github.com/jmmk/rustcap\\"
repository = \\"https://github.com/jmmk/rustcap\\"
authors = [\\"Michael McLellan <jmikem825@gmail.com>\\"]
keywords = [\\"pcap\\", \\"libpcap\\"]
license = \\"MIT\\"
readme = \\"README.md\\"
documentation = \\"https://docs.rs/rustcap\\"
[workspace]
members = [\\"pcap-sys\\"]
[dependencies]
libc = \\"=0.2.43\\"
bitflags = \\"1.0.4\\"
pcap-sys = { version = \\"=0.1\\", path = \\"pcap-sys\\" }
pnet = { version = \\"0.21.0\\", optional = true, default-features = false}
git_dep_with_version = { version = \\"0.1.0\\", git = \\"https://github.com/foo/bar\\" }
git_dep = { git = \\"https://github.com/foo/bar\\" }
same_version_1__ = \\"0.0.0\\"
same_version_1_ = \\"0.0.0\\"
same_version_1 = \\"1.2.3\\"
[target.'cfg(windows)'.dependencies.winapi]
version = \\"=0.3.6\\"
features = [\\"ws2def\\", \\"ws2ipdef\\"]
[target.'cfg(target_arch = \\"wasm32\\")'.dependencies]
wasm-bindgen = \\"0.2.37\\"
js-sys = \\"0.3.14\\"
js_relative_import = { path = \\"../../common/js_relative_import\\" }
[target.'cfg(target_arch = \\"wasm32\\")'.dependencies.web-sys]
version = \\"0.3.14\\"
features = [
\\"AudioBuffer\\",
\\"AudioBufferSourceNode\\",
\\"AudioContext\\",
\\"AudioDestinationNode\\",
\\"AudioNode\\",
]
[dependencies.dep1]
version = \\"0.4.2\\"
[features]
libpnet = [\\"pnet\\"]
[dev-dependencies]
# libc = \\"0.1.1\\"
# pnet = { version = \\"0.19.0\\", optional = true, default-features = false}
# [target.'cfg(windows)'.dependencies.winapi]
# version = \\"0.3.0\\"
# features = [\\"ws2def\\", \\"ws2ipdef\\"]"
`;
exports[`lib/manager/cargo/update updateDependency() updates platform specific dependency 1`] = `
"[package]
name = \\"rustcap\\"
version = \\"0.1.2\\"
description = \\"Wrapper for libpcap\\"
homepage = \\"https://github.com/jmmk/rustcap\\"
repository = \\"https://github.com/jmmk/rustcap\\"
authors = [\\"Michael McLellan <jmikem825@gmail.com>\\"]
keywords = [\\"pcap\\", \\"libpcap\\"]
license = \\"MIT\\"
readme = \\"README.md\\"
documentation = \\"https://docs.rs/rustcap\\"
[workspace]
members = [\\"pcap-sys\\"]
[dependencies]
libc = \\"=0.2.43\\"
bitflags = \\"1.0.4\\"
pcap-sys = { version = \\"=0.1\\", path = \\"pcap-sys\\" }
pnet = { version = \\"0.21.0\\", optional = true, default-features = false}
git_dep_with_version = { version = \\"0.1.0\\", git = \\"https://github.com/foo/bar\\" }
git_dep = { git = \\"https://github.com/foo/bar\\" }
same_version_1__ = \\"0.0.0\\"
same_version_1_ = \\"0.0.0\\"
same_version_1 = \\"0.0.0\\"
[target.'cfg(windows)'.dependencies.winapi]
version = \\"0.4.0\\"
features = [\\"ws2def\\", \\"ws2ipdef\\"]
[target.'cfg(target_arch = \\"wasm32\\")'.dependencies]
wasm-bindgen = \\"0.2.37\\"
js-sys = \\"0.3.14\\"
js_relative_import = { path = \\"../../common/js_relative_import\\" }
[target.'cfg(target_arch = \\"wasm32\\")'.dependencies.web-sys]
version = \\"0.3.14\\"
features = [
\\"AudioBuffer\\",
\\"AudioBufferSourceNode\\",
\\"AudioContext\\",
\\"AudioDestinationNode\\",
\\"AudioNode\\",
]
[dependencies.dep1]
version = \\"0.4.2\\"
[features]
libpnet = [\\"pnet\\"]
[dev-dependencies]
# libc = \\"0.1.1\\"
# pnet = { version = \\"0.19.0\\", optional = true, default-features = false}
# [target.'cfg(windows)'.dependencies.winapi]
# version = \\"0.3.0\\"
# features = [\\"ws2def\\", \\"ws2ipdef\\"]"
`;

View file

@ -1,5 +1,4 @@
import { extractPackageFile } from './extract';
import { updateDependency } from './update';
import { updateArtifacts } from './artifacts';
import { LANGUAGE_RUST } from '../../constants/languages';
import * as cargoVersioning from '../../versioning/cargo';
@ -8,7 +7,7 @@ const language = LANGUAGE_RUST;
// TODO: Support this
export const supportsLockFileMaintenance = false;
export { extractPackageFile, updateArtifacts, language, updateDependency };
export { extractPackageFile, updateArtifacts, language };
export const defaultConfig = {
commitMessageTopic: 'Rust crate {{depName}}',

View file

@ -1,252 +0,0 @@
import { readFileSync } from 'fs';
import { updateDependency } from './update';
const cargo1toml = readFileSync(
'lib/manager/cargo/__fixtures__/Cargo.1.toml',
'utf8'
);
const cargo4toml = readFileSync(
'lib/manager/cargo/__fixtures__/Cargo.4.toml',
'utf8'
);
const cargo5toml = readFileSync(
'lib/manager/cargo/__fixtures__/Cargo.5.toml',
'utf8'
);
describe('lib/manager/cargo/update', () => {
describe('updateDependency()', () => {
let config;
beforeEach(() => {
config = { managerData: {} };
});
it('returns same for invalid toml', () => {
const cargotoml = 'invalid toml !#$#';
expect(
updateDependency({ fileContent: cargotoml, upgrade: config })
).toEqual(cargotoml);
});
it('returns same for null upgrade', () => {
const cargotoml = '[dependencies]\n';
expect(
updateDependency({ fileContent: cargotoml, upgrade: null })
).toEqual(cargotoml);
});
it('returns same if version has not changed', () => {
const cargotoml = '[dependencies]\n';
expect(
updateDependency({ fileContent: cargotoml, upgrade: null })
).toEqual(cargotoml);
const upgrade = {
depName: 'libc',
depType: 'dependencies',
managerData: { nestedVersion: false },
newValue: '=0.2.43',
};
expect(
updateDependency({ fileContent: cargo1toml, upgrade })
).not.toBeNull();
expect(updateDependency({ fileContent: cargo1toml, upgrade })).toBe(
cargo1toml
);
});
it('returns same for invalid target', () => {
const cargotoml = '[dependencies]\n';
expect(
updateDependency({ fileContent: cargotoml, upgrade: null })
).toEqual(cargotoml);
const upgrade = {
depName: 'platform-specific-dep',
depType: 'dependencies',
managerData: { nestedVersion: false },
target: 'foobar',
newValue: '1.2.3',
};
expect(
updateDependency({ fileContent: cargo1toml, upgrade })
).not.toBeNull();
expect(updateDependency({ fileContent: cargo1toml, upgrade })).toBe(
cargo1toml
);
});
it('returns same for invalid depType', () => {
const cargotoml = '[dependencies]\n';
expect(
updateDependency({ fileContent: cargotoml, upgrade: null })
).toEqual(cargotoml);
const upgrade = {
depName: 'libc',
depType: 'foobar',
managerData: { nestedVersion: false },
newValue: '1.2.3',
};
expect(
updateDependency({ fileContent: cargo1toml, upgrade })
).not.toBeNull();
expect(updateDependency({ fileContent: cargo1toml, upgrade })).toBe(
cargo1toml
);
});
it('returns same for invalid depName', () => {
const cargotoml = '[dependencies]\n';
expect(
updateDependency({ fileContent: cargotoml, upgrade: null })
).toEqual(cargotoml);
const upgrade = {
depName: 'does not exist',
depType: 'dependencies',
managerData: { nestedVersion: false },
newValue: '1.2.3',
};
expect(
updateDependency({ fileContent: cargo1toml, upgrade })
).not.toBeNull();
expect(updateDependency({ fileContent: cargo1toml, upgrade })).toBe(
cargo1toml
);
});
it('updates normal dependency', () => {
const upgrade = {
depName: 'libc',
depType: 'dependencies',
managerData: { nestedVersion: false },
newValue: '0.3.0',
};
expect(
updateDependency({ fileContent: cargo1toml, upgrade })
).not.toBeNull();
expect(updateDependency({ fileContent: cargo1toml, upgrade })).not.toBe(
cargo1toml
);
expect(
updateDependency({ fileContent: cargo1toml, upgrade })
).toMatchSnapshot();
});
it('updates normal dependency with mismatch on first try', () => {
const upgrade = {
depName: 'same_version_1',
depType: 'dependencies',
managerData: { nestedVersion: false },
newValue: '1.2.3',
};
expect(
updateDependency({ fileContent: cargo1toml, upgrade })
).not.toBeNull();
expect(updateDependency({ fileContent: cargo1toml, upgrade })).not.toBe(
cargo1toml
);
expect(
updateDependency({ fileContent: cargo1toml, upgrade })
).toMatchSnapshot();
});
it('updates nested version dependency', () => {
const upgrade = {
depName: 'pcap-sys',
depType: 'dependencies',
managerData: { nestedVersion: true },
newValue: '0.2.0',
};
expect(
updateDependency({ fileContent: cargo1toml, upgrade })
).not.toBeNull();
expect(updateDependency({ fileContent: cargo1toml, upgrade })).not.toBe(
cargo1toml
);
expect(
updateDependency({ fileContent: cargo1toml, upgrade })
).toMatchSnapshot();
});
it('updates platform specific dependency', () => {
const upgrade = {
depName: 'winapi',
target: 'cfg(windows)',
depType: 'dependencies',
managerData: { nestedVersion: true },
newValue: '0.4.0',
};
expect(
updateDependency({ fileContent: cargo1toml, upgrade })
).not.toBeNull();
expect(updateDependency({ fileContent: cargo1toml, upgrade })).not.toBe(
cargo1toml
);
expect(
updateDependency({ fileContent: cargo1toml, upgrade })
).toMatchSnapshot();
});
it('handles invalid standard tables gracefully', () => {
const upgrade = {
depName: 'dep5',
managerData: { nestedVersion: true },
depType: 'dependencies',
newValue: '2.0.0',
};
expect(updateDependency({ fileContent: cargo4toml, upgrade })).toEqual(
cargo4toml
);
});
it('does not update in case of error', () => {
const upgrade = {
depName: 'libc',
devType: 'dev-dependencies', // Wrong devType
managerData: { nestedVersion: false },
newValue: '0.3.0',
};
expect(updateDependency({ fileContent: cargo1toml, upgrade })).toEqual(
cargo1toml
);
});
it('does not update in case of error', () => {
const upgrade = {
depName: 'libc',
devType: 'dependencies',
managerData: { nestedVersion: true }, // Should be false
newValue: '0.3.0',
};
expect(updateDependency({ fileContent: cargo1toml, upgrade })).toEqual(
cargo1toml
);
});
it('does not update in case of error', () => {
const upgrade = {
depName: 'pcap-sys',
devType: 'dependencies',
managerData: { nestedVersion: false }, // Should be true
newValue: '0.3.0',
};
expect(updateDependency({ fileContent: cargo1toml, upgrade })).toEqual(
cargo1toml
);
});
it('updates platform specific normal dependency', () => {
const upgrade = {
depName: 'wasm-bindgen',
depType: 'dependencies',
managerData: { nestedVersion: false },
target: 'cfg(target_arch = "wasm32")',
newValue: '0.3.0',
};
expect(
updateDependency({ fileContent: cargo5toml, upgrade })
).not.toBeNull();
expect(updateDependency({ fileContent: cargo5toml, upgrade })).not.toBe(
cargo5toml
);
});
it('updates platform specific table dependency', () => {
const upgrade = {
depName: 'web-sys',
managerData: { nestedVersion: true },
depType: 'dependencies',
target: 'cfg(target_arch = "wasm32")',
newValue: '0.4.0',
};
expect(
updateDependency({ fileContent: cargo5toml, upgrade })
).not.toBeNull();
expect(updateDependency({ fileContent: cargo5toml, upgrade })).not.toBe(
cargo5toml
);
});
});
});

View file

@ -1,114 +0,0 @@
import { isEqual } from 'lodash';
import { parse } from 'toml';
import { logger } from '../../logger';
import { UpdateDependencyConfig } from '../common';
import { CargoConfig, CargoSection } from './types';
import { matchAt, replaceAt } from '../../util/string';
export function updateDependency({
fileContent,
upgrade,
}: UpdateDependencyConfig): string {
logger.trace({ config: upgrade }, 'poetry.updateDependency()');
if (!upgrade) {
return fileContent;
}
const { target, depType, depName, newValue, managerData } = upgrade;
const { nestedVersion } = managerData;
let parsedContent: CargoConfig;
try {
parsedContent = parse(fileContent);
} catch (err) {
logger.debug({ err }, 'Error parsing Cargo.toml file');
return fileContent;
}
let section: CargoSection;
if (target) {
section = parsedContent.target[target];
if (section) {
section = section[depType];
}
} else {
section = parsedContent[depType];
}
if (!section) {
if (target) {
logger.debug(
{ config: upgrade },
`Error: Section [target.${target}.${depType}] doesn't exist in Cargo.toml file, update failed`
);
} else {
logger.debug(
{ config: upgrade },
`Error: Section [${depType}] doesn't exist in Cargo.toml file, update failed`
);
}
return fileContent;
}
let oldVersion: any;
const oldDep = section[depName];
if (!oldDep) {
logger.debug(
{ config: upgrade },
`Could not get version of dependency ${depName}, update failed (most likely name is invalid)`
);
return fileContent;
}
oldVersion = section[depName];
// if (typeof oldVersion !== 'string') {
// if (oldVersion.version) {
// oldVersion = oldVersion.version;
// } else {
// oldVersion = null;
// }
// }
if (nestedVersion) {
oldVersion = oldVersion.version;
}
if (!oldVersion) {
logger.debug(
{ config: upgrade },
`Could not get version of dependency ${depName}, update failed (most likely name is invalid)`
);
return fileContent;
}
if (oldVersion === newValue) {
logger.debug('Version is already updated');
return fileContent;
}
if (nestedVersion) {
section[depName].version = newValue;
} else {
section[depName] = newValue;
}
if (target) {
parsedContent.target[target][depType] = section;
} else {
parsedContent[depType] = section;
}
const searchString = `"${oldVersion}"`;
const newString = `"${newValue}"`;
let newFileContent = fileContent;
let searchIndex = fileContent.indexOf(`${depName}`) + depName.length;
for (; searchIndex < fileContent.length; searchIndex += 1) {
// First check if we have a hit for the old version
if (matchAt(fileContent, searchIndex, searchString)) {
logger.trace(`Found match at index ${searchIndex}`);
// Now test if the result matches
const testContent = replaceAt(
fileContent,
searchIndex,
searchString,
newString
);
// Compare the parsed toml structure of old and new
if (isEqual(parsedContent, parse(testContent))) {
newFileContent = testContent;
break;
} else {
logger.debug('Mismatched replace at searchIndex ' + searchIndex);
}
}
}
return newFileContent;
}