Skip to content

Commit

Permalink
Fixed a bug in writing to pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
David-OConnor committed Sep 30, 2019
1 parent 5e3e451 commit 62fee15
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 41 deletions.
5 changes: 1 addition & 4 deletions src/dep_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,7 @@ pub fn resolve(
None => {
// We consider the possibility there's a compatible version
// that wasn't one of the best-per-req we queried.
println!(
"⛏️ Digging deeper to resolve dependencies for {}...",
name
);
println!("⛏️ Digging deeper to resolve dependencies for {}...", name);

// I think we should query with the raw name, not fmted?
let versions = &version_cache.get(&name).unwrap().2;
Expand Down
123 changes: 100 additions & 23 deletions src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,40 +131,38 @@ fn update_cfg(cfg_data: String, added: &[Req], added_dev: &[Req]) -> String {
for (i, line) in cfg_data.lines().enumerate() {
if &line.replace(" ", "") == "[tool.pyflow.dependencies]" {
dep_start = i + 1;
in_dep = true;
in_dev_dep = false;
if in_dev_dep {
dev_dep_end = i;
dev_dep_end = i - 1;
}
in_dep = true;
in_dev_dep = false;
continue; // Continue so this line doesn't trigger the section's end.
}

if &line.replace(" ", "") == "[tool.pyflow.dev-dependencies]" {
dev_dep_start = i + 1;
in_dep = false;
in_dev_dep = true;
if in_dep {
dep_end = i;
dep_end = i - 1;
}
in_dep = false;
in_dev_dep = true;
continue;
}

// We've found the end of the dependencies section.
if in_dep && (sect_re.is_match(line) || i == lines_vec.len() - 1) {
in_dep = false;
dep_end = i;
dep_end = i - 1;
}

println!("len: {:?}, &i: {:?}", &lines_vec.len(), &i);
if in_dev_dep && (sect_re.is_match(line) || i == lines_vec.len() - 1) {
in_dev_dep = false;
dev_dep_end = i;
dev_dep_end = i - 1;
}
}

println!("START: {:?}, end: {:?}", &dev_dep_start, &dev_dep_end);
let mut insertion_pt = dep_start;
if dep_end != 0 {
if dep_start != 0 {
for i in dep_start..dep_end + 1 {
let line = lines_vec[i];
if !line.is_empty() {
Expand All @@ -174,7 +172,7 @@ fn update_cfg(cfg_data: String, added: &[Req], added_dev: &[Req]) -> String {
}

let mut dev_insertion_pt = dev_dep_start;
if dev_dep_end != 0 {
if dev_dep_start != 0 {
for i in dev_dep_start..dev_dep_end + 1 {
let line = lines_vec[i];
if !line.is_empty() {
Expand All @@ -184,38 +182,44 @@ fn update_cfg(cfg_data: String, added: &[Req], added_dev: &[Req]) -> String {
}

for (i, line) in cfg_data.lines().enumerate() {
result.push_str(line);
result.push_str("\n");

if i == insertion_pt && insertion_pt != 0 {
if i == insertion_pt && dep_start != 0 {
for req in added {
result.push_str(&req.to_cfg_string());
result.push_str("\n");
}
}
if i == dev_insertion_pt && dev_insertion_pt != 0 {
if i == dev_insertion_pt && dev_dep_start != 0 {
for req in added_dev {
result.push_str(&req.to_cfg_string());
result.push_str("\n");
}
}
result.push_str(line);
result.push_str("\n");
}

// If the sectcions don't exist, create them.
// If the sections don't exist, create them.
// todo: Adjust start pad as needed so there's exactly two blank lines before adding the section.
if dep_start == 0 {
result.push_str("\n\n[tool.pyflow.dependencies]\n");
// todo: Should add dependencies section before dev deps section.
result.push_str("\n[tool.pyflow.dependencies]\n");
for req in added {
result.push_str(&req.to_cfg_string());
result.push_str("\n");
}
if dev_dep_start != 0 {
// We only need to add one end-of-file pad.
result.push_str("\n");
}
}

if dev_dep_start == 0 {
result.push_str("\n\n[tool.pyflow.dev-dependencies]\n");
result.push_str("\n[tool.pyflow.dev-dependencies]\n");
for req in added_dev {
result.push_str(&req.to_cfg_string());
result.push_str("\n");
}
result.push_str("\n");
}

result
Expand Down Expand Up @@ -440,7 +444,7 @@ dev_a = "^1.17.2"
"#;

const BASELINE_NO_DEPS: &str = r#"
const BASELINE_NO_DEPS: &str = r#"
[tool.pyflow]
name = ""
Expand All @@ -450,14 +454,27 @@ dev_a = "^1.17.2"
"#;

const BASELINE_NO_DEV_DEPS: &str = r#"
const BASELINE_NO_DEV_DEPS: &str = r#"
[tool.pyflow]
name = ""
[tool.pyflow.dependencies]
a = "^0.3.5"
"#;

const BASELINE_EMPTY_DEPS: &str = r#"
[tool.pyflow]
name = ""
[tool.pyflow.dependencies]
[tool.pyflow.dev-dependencies]
dev_a = "^1.17.2"
"#;

#[test]
Expand All @@ -472,7 +489,7 @@ a = "^0.3.5"
);

let expected = r#"
[tool.pyflow]
[tool.pyflow]
name = ""
Expand All @@ -482,6 +499,66 @@ b = "^0.0.1"
c = "^0.0.1"
[tool.pyflow.dev-dependencies]
dev_a = "^1.17.2"
dev_b = "^0.0.1"
"#;

assert_eq!(expected, &actual);
}

#[test]
fn add_deps_no_dev_deps_sect() {
let actual = update_cfg(
BASELINE_NO_DEV_DEPS.into(),
&[
Req::new("b".into(), base_constrs()),
Req::new("c".into(), base_constrs()),
],
&[Req::new("dev_b".into(), base_constrs())],
);

let expected = r#"
[tool.pyflow]
name = ""
[tool.pyflow.dependencies]
a = "^0.3.5"
b = "^0.0.1"
c = "^0.0.1"
[tool.pyflow.dev-dependencies]
dev_b = "^0.0.1"
"#;

assert_eq!(expected, &actual);
}

#[test]
fn add_deps_baseline_empty_deps() {
let actual = update_cfg(
BASELINE_EMPTY_DEPS.into(),
&[
Req::new("b".into(), base_constrs()),
Req::new("c".into(), base_constrs()),
],
&[Req::new("dev_b".into(), base_constrs())],
);

let expected = r#"
[tool.pyflow]
name = ""
[tool.pyflow.dependencies]
b = "^0.0.1"
c = "^0.0.1"
[tool.pyflow.dev-dependencies]
dev_a = "^1.17.2"
dev_b = "^0.0.1"
Expand Down
12 changes: 2 additions & 10 deletions src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,17 +333,9 @@ pub fn uninstall(name_ins: &str, vers_ins: &Version, lib_path: &Path) {
#[cfg(target_os = "windows")]
println!("Uninstalling {}: {}...", name_ins, vers_ins.to_string());
#[cfg(target_os = "linux")]
println!(
"🗑 Uninstalling {}: {}...",
name_ins,
vers_ins.to_string()
);
println!("🗑 Uninstalling {}: {}...", name_ins, vers_ins.to_string());
#[cfg(target_os = "macos")]
println!(
"🗑 Uninstalling {}: {}...",
name_ins,
vers_ins.to_string()
);
println!("🗑 Uninstalling {}: {}...", name_ins, vers_ins.to_string());

// Uninstall the package
// package folders appear to be lowercase, while metadata keeps the package title's casing.
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,12 @@ package_url = "https://test.pypi.org/legacy/"
[tool.pyflow.dependencies]
[tool.pyflow.dev-dependencies]
"##,
name
);

// todo: flesh readme out
let readme_init = &format!("# {}", name);
let readme_init = &format!("# {}\n\n{}", name, "(A description)");

fs::write(&format!("{}/.gitignore", name), gitignore_init)?;
fs::write(&format!("{}/pyproject.toml", name), pyproject_init)?;
Expand Down
10 changes: 8 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,14 @@ pub fn merge_reqs(
// version.
for added_req in added_reqs_unique.iter_mut() {
if added_req.constraints.is_empty() {
let (_, vers, _) = dep_resolution::get_version_info(&added_req.name)
.expect("Problem getting latest version of the package you added.");
let (_, vers, _) = match dep_resolution::get_version_info(&added_req.name) {
Ok(r) => r,
Err(_) => {
abort("Problem getting latest version of the package you added. Is it spelled correctly? Is the internet OK?");
unreachable!()
}
};

added_req.constraints.push(Constraint::new(
ReqType::Caret,
// Version::new(vers.major, vers.minor, vers.patch),
Expand Down

0 comments on commit 62fee15

Please sign in to comment.