fix(package): Ignore setup.cfg attr: and file: (#3054)

The setup.cfg version key allows "attr:" and "file:" directives to load
the actual value from elsewhere [1]. Treat these as if no version was
found, since the text of the directive isn't the version number we're
after.

[1]: https://setuptools.readthedocs.io/en/latest/userguide/declarative_config.html#specifying-values
This commit is contained in:
Tom Most 2021-09-14 10:17:05 -07:00 committed by GitHub
parent 5d0a38aca3
commit bbb8d3c357
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 1 deletions

View File

@ -84,8 +84,12 @@ fn get_setup_cfg_version(context: &Context, config: &PackageConfig) -> Option<St
let ini = Ini::load_from_str(&file_contents).ok()?;
let raw_version = ini.get_from(Some("metadata"), "version")?;
if raw_version.starts_with("attr:") || raw_version.starts_with("file:") {
None
} else {
format_version(raw_version, config.version_format)
}
}
fn get_gradle_version(context: &Context, config: &PackageConfig) -> Option<String> {
let file_contents = utils::read_file(context.current_dir.join("build.gradle")).ok()?;
@ -613,6 +617,34 @@ license = "MIT"
project_dir.close()
}
#[test]
fn test_extract_setup_cfg_version_attr() -> io::Result<()> {
let config_name = "setup.cfg";
let config_content = String::from(
"[metadata]
version = attr: mymod.__version__",
);
let project_dir = create_project_dir()?;
fill_config(&project_dir, config_name, Some(&config_content))?;
expect_output(&project_dir, None, None);
project_dir.close()
}
#[test]
fn test_extract_setup_cfg_version_file() -> io::Result<()> {
let config_name = "setup.cfg";
let config_content = String::from(
"[metadata]
version = file: version.txt",
);
let project_dir = create_project_dir()?;
fill_config(&project_dir, config_name, Some(&config_content))?;
expect_output(&project_dir, None, None);
project_dir.close()
}
#[test]
fn test_extract_gradle_version_single_quote() -> io::Result<()> {
let config_name = "build.gradle";