|
| 1 | +use assert_cmd::prelude::*; |
| 2 | +use command_extra::CommandExtra; |
| 3 | +use pacquet_testing_utils::bin::CommandTempCwd; |
| 4 | +use pretty_assertions::assert_eq; |
| 5 | +use std::{ |
| 6 | + fs, |
| 7 | + path::{Path, PathBuf}, |
| 8 | + process::Command, |
| 9 | +}; |
| 10 | + |
| 11 | +fn canonicalize(path: &Path) -> PathBuf { |
| 12 | + dunce::canonicalize(path).expect("canonicalize path") |
| 13 | +} |
| 14 | + |
| 15 | +#[test] |
| 16 | +fn prefix_prints_the_local_prefix_dir() { |
| 17 | + let CommandTempCwd { pacquet, root, workspace, .. } = CommandTempCwd::init(); |
| 18 | + fs::write(workspace.join("package.json"), r#"{ "name": "root-pkg" }"#) |
| 19 | + .expect("write package.json"); |
| 20 | + |
| 21 | + let output = pacquet.with_args(["prefix"]).output().expect("run pacquet prefix"); |
| 22 | + dbg!(&output); |
| 23 | + assert!(output.status.success(), "pacquet prefix should succeed"); |
| 24 | + |
| 25 | + let expected = format!("{}\n", canonicalize(&workspace).display()); |
| 26 | + assert_eq!(String::from_utf8_lossy(&output.stdout), expected); |
| 27 | + |
| 28 | + drop(root); |
| 29 | +} |
| 30 | + |
| 31 | +#[test] |
| 32 | +fn prefix_walks_up_to_find_package_json() { |
| 33 | + let CommandTempCwd { root, workspace, .. } = CommandTempCwd::init(); |
| 34 | + fs::write(workspace.join("package.json"), r#"{ "name": "root-pkg" }"#) |
| 35 | + .expect("write package.json"); |
| 36 | + |
| 37 | + let member = workspace.join("sub/dir/deep"); |
| 38 | + fs::create_dir_all(&member).expect("create member dir"); |
| 39 | + |
| 40 | + let pacquet_out = Command::cargo_bin("pacquet") |
| 41 | + .expect("find the pacquet binary") |
| 42 | + .with_current_dir(&member) |
| 43 | + .with_args(["prefix"]) |
| 44 | + .output() |
| 45 | + .expect("run pacquet prefix in the subdir"); |
| 46 | + assert!(pacquet_out.status.success(), "pacquet prefix should succeed in the subdir"); |
| 47 | + |
| 48 | + let expected = format!("{}\n", canonicalize(&workspace).display()); |
| 49 | + assert_eq!(String::from_utf8_lossy(&pacquet_out.stdout), expected); |
| 50 | + |
| 51 | + drop(root); |
| 52 | +} |
| 53 | + |
| 54 | +#[test] |
| 55 | +fn prefix_walks_up_from_node_modules() { |
| 56 | + let CommandTempCwd { root, workspace, .. } = CommandTempCwd::init(); |
| 57 | + fs::write(workspace.join("package.json"), r#"{ "name": "root-pkg" }"#) |
| 58 | + .expect("write package.json"); |
| 59 | + |
| 60 | + let nm_dir = workspace.join("node_modules/some-pkg"); |
| 61 | + fs::create_dir_all(&nm_dir).expect("create node_modules/some-pkg dir"); |
| 62 | + |
| 63 | + let pacquet_out = Command::cargo_bin("pacquet") |
| 64 | + .expect("find the pacquet binary") |
| 65 | + .with_current_dir(&nm_dir) |
| 66 | + .with_args(["prefix"]) |
| 67 | + .output() |
| 68 | + .expect("run pacquet prefix from inside node_modules"); |
| 69 | + assert!(pacquet_out.status.success(), "pacquet prefix should succeed from inside node_modules"); |
| 70 | + |
| 71 | + let expected = format!("{}\n", canonicalize(&workspace).display()); |
| 72 | + assert_eq!(String::from_utf8_lossy(&pacquet_out.stdout), expected); |
| 73 | + |
| 74 | + drop(root); |
| 75 | +} |
| 76 | + |
| 77 | +#[test] |
| 78 | +fn prefix_global_is_not_supported_yet() { |
| 79 | + let CommandTempCwd { pacquet, root, .. } = CommandTempCwd::init(); |
| 80 | + |
| 81 | + let output = pacquet.with_args(["prefix", "-g"]).output().expect("run pacquet prefix -g"); |
| 82 | + dbg!(&output); |
| 83 | + assert!(!output.status.success(), "pacquet prefix -g should fail until global support lands"); |
| 84 | + |
| 85 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 86 | + assert!(stderr.contains("not supported yet"), "stderr should explain the gap: {stderr}"); |
| 87 | + |
| 88 | + drop(root); |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn prefix_resolves_from_a_workspace_subdir() { |
| 93 | + let CommandTempCwd { root, workspace, .. } = CommandTempCwd::init(); |
| 94 | + |
| 95 | + fs::write(workspace.join("package.json"), r#"{ "name": "wsroot", "version": "1.0.0" }"#) |
| 96 | + .expect("write workspace-root package.json"); |
| 97 | + let member = workspace.join("packages/foo"); |
| 98 | + fs::create_dir_all(&member).expect("create workspace member dir"); |
| 99 | + fs::write(member.join("package.json"), r#"{ "name": "foo", "version": "1.0.0" }"#) |
| 100 | + .expect("write member package.json"); |
| 101 | + |
| 102 | + let sub_member = member.join("src/utils"); |
| 103 | + fs::create_dir_all(&sub_member).expect("create sub_member dir"); |
| 104 | + |
| 105 | + let pacquet_out = Command::cargo_bin("pacquet") |
| 106 | + .expect("find the pacquet binary") |
| 107 | + .with_current_dir(&sub_member) |
| 108 | + .with_args(["prefix"]) |
| 109 | + .output() |
| 110 | + .expect("run pacquet prefix in the subdir"); |
| 111 | + assert!(pacquet_out.status.success(), "pacquet prefix should succeed in the subdir"); |
| 112 | + |
| 113 | + // From the workspace subdir the nearest package.json parent is the member |
| 114 | + let expected = format!("{}\n", canonicalize(&member).display()); |
| 115 | + assert_eq!(String::from_utf8_lossy(&pacquet_out.stdout), expected); |
| 116 | + |
| 117 | + drop(root); |
| 118 | +} |
0 commit comments