|
| 1 | +//! `sc remote add` parse-validates the native network URL schemes at add time, |
| 2 | +//! so a typo fails here and not at the first fetch (P32 follow-on). |
| 3 | +
|
| 4 | +use std::process::Command; |
| 5 | + |
| 6 | +fn sc(dir: &std::path::Path, args: &[&str]) -> std::process::Output { |
| 7 | + Command::new(env!("CARGO_BIN_EXE_sc")) |
| 8 | + .args(args) |
| 9 | + .current_dir(dir) |
| 10 | + .output() |
| 11 | + .expect("sc runs") |
| 12 | +} |
| 13 | + |
| 14 | +fn repo(tag: &str) -> std::path::PathBuf { |
| 15 | + let root = |
| 16 | + std::env::temp_dir().join(format!("scl-cli-remote-add-{tag}-{}", std::process::id())); |
| 17 | + let _ = std::fs::remove_dir_all(&root); |
| 18 | + std::fs::create_dir_all(&root).unwrap(); |
| 19 | + assert!(sc(&root, &["init"]).status.success()); |
| 20 | + root |
| 21 | +} |
| 22 | + |
| 23 | +#[test] |
| 24 | +fn malformed_http_urls_are_refused_at_add_time_and_good_ones_recorded() { |
| 25 | + let root = repo("http"); |
| 26 | + for bad in [ |
| 27 | + "sc+http://", |
| 28 | + "sc+http:///repo", |
| 29 | + "sc+https://host:notaport/repo", |
| 30 | + "sc+http://host:99999/repo", |
| 31 | + "sc+https:/host/repo", // near-miss scheme: one slash |
| 32 | + "sc+http:host/repo", |
| 33 | + ] { |
| 34 | + let out = sc(&root, &["remote", "add", "origin", bad]); |
| 35 | + assert!(!out.status.success(), "{bad} should be refused"); |
| 36 | + let list = sc(&root, &["remote", "list"]); |
| 37 | + assert!( |
| 38 | + String::from_utf8_lossy(&list.stdout).trim().is_empty(), |
| 39 | + "{bad} must not be recorded" |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + for (name, good) in [ |
| 44 | + ("plain", "sc+http://127.0.0.1:7777/repo"), |
| 45 | + ("secure", "sc+https://example.com/repo"), |
| 46 | + ] { |
| 47 | + let out = sc(&root, &["remote", "add", name, good]); |
| 48 | + assert!( |
| 49 | + out.status.success(), |
| 50 | + "{good}: {}", |
| 51 | + String::from_utf8_lossy(&out.stderr) |
| 52 | + ); |
| 53 | + } |
| 54 | + let list = String::from_utf8_lossy(&sc(&root, &["remote", "list"]).stdout).into_owned(); |
| 55 | + assert!(list.contains("sc+http://127.0.0.1:7777/repo"), "{list}"); |
| 56 | + assert!(list.contains("sc+https://example.com/repo"), "{list}"); |
| 57 | + |
| 58 | + std::fs::remove_dir_all(&root).unwrap(); |
| 59 | + assert!(!root.exists(), "test must leave no residue"); |
| 60 | +} |
| 61 | + |
| 62 | +/// With no path, `sc serve fingerprint` finds the repo from a subdirectory, |
| 63 | +/// and outside any repo it refuses instead of minting. |
| 64 | +#[test] |
| 65 | +fn serve_fingerprint_discovers_the_repo_and_refuses_outside_one() { |
| 66 | + let root = repo("fingerprint"); |
| 67 | + let sub = root.join("a/b"); |
| 68 | + std::fs::create_dir_all(&sub).unwrap(); |
| 69 | + let out = sc(&sub, &["serve", "fingerprint"]); |
| 70 | + assert!( |
| 71 | + out.status.success(), |
| 72 | + "{}", |
| 73 | + String::from_utf8_lossy(&out.stderr) |
| 74 | + ); |
| 75 | + assert!(String::from_utf8_lossy(&out.stdout).starts_with("sha256:")); |
| 76 | + assert!( |
| 77 | + root.join(".sc/serve-tls").is_dir(), |
| 78 | + "minted in the repo root" |
| 79 | + ); |
| 80 | + assert!(!sub.join(".sc").exists(), "never in the subdirectory"); |
| 81 | + |
| 82 | + let bare = |
| 83 | + std::env::temp_dir().join(format!("scl-cli-fingerprint-bare-{}", std::process::id())); |
| 84 | + let _ = std::fs::remove_dir_all(&bare); |
| 85 | + std::fs::create_dir_all(&bare).unwrap(); |
| 86 | + let out = sc(&bare, &["serve", "fingerprint"]); |
| 87 | + assert!(!out.status.success()); |
| 88 | + assert!(!bare.join(".sc").exists(), "nothing may be created"); |
| 89 | + |
| 90 | + std::fs::remove_dir_all(&root).unwrap(); |
| 91 | + std::fs::remove_dir_all(&bare).unwrap(); |
| 92 | + assert!( |
| 93 | + !root.exists() && !bare.exists(), |
| 94 | + "test must leave no residue" |
| 95 | + ); |
| 96 | +} |
0 commit comments