Skip to content

Commit e16e953

Browse files
committed
tests: resolve nix store path dynamically
The profile tests hardcoded /nix/var/nix/profiles/default/ as nix_store_path, which doesn't exist on systems where nix is installed via a user profile. Resolve the actual store path by canonicalizing the nix binary found in PATH.
1 parent edf2794 commit e16e953

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

src/profile/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ impl Profile<'_> {
7777
}
7878
}
7979

80+
/// Find the nix store path containing bin/nix by resolving the `nix` binary
81+
/// through PATH. The tests need this because `nix_store_path` is used to
82+
/// construct paths like `nix_store_path.join("bin/nix")`, so it must point to
83+
/// the actual store path rather than a hardcoded profile symlink that may not
84+
/// exist (e.g. /nix/var/nix/profiles/default/).
85+
#[cfg(test)]
86+
fn find_nix_store_path() -> PathBuf {
87+
let nix_bin = crate::util::which("nix").expect("nix not found in PATH");
88+
let real_path = std::fs::canonicalize(nix_bin).expect("canonicalize nix path");
89+
// .../bin/nix -> ...
90+
real_path.parent().unwrap().parent().unwrap().to_path_buf()
91+
}
92+
8093
pub fn get_profile_backend_type(profile: &std::path::Path) -> Option<BackendType> {
8194
// If the file has a manifest.json, that means `nix profile` touched it, and ONLY `nix profile` can touch it.
8295
if std::fs::metadata(profile.join("manifest.json")).is_ok() {

src/profile/nixenv/tests.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,16 @@ fn test_detect_intersection() {
7373
return;
7474
}
7575

76+
let nix_store_path = super::super::find_nix_store_path();
7677
let profile = tempfile::tempdir().unwrap();
7778
let profile_path = profile.path().join("profile");
7879

7980
let tree_1 = sample_tree("foo", "foo", "a");
8081
let tree_2 = sample_tree("bar", "foo", "b");
8182

8283
(NixEnv {
83-
nix_store_path: Path::new("/nix/var/nix/profiles/default/"),
84-
nss_ca_cert_path: Path::new("/nix/var/nix/profiles/default/"),
84+
nix_store_path: &nix_store_path,
85+
nss_ca_cert_path: &nix_store_path,
8586
profile: &profile_path,
8687
pkgs: &[&tree_1, &tree_2],
8788
})
@@ -95,15 +96,16 @@ fn test_no_intersection() {
9596
return;
9697
}
9798

99+
let nix_store_path = super::super::find_nix_store_path();
98100
let profile = tempfile::tempdir().unwrap();
99101
let profile_path = profile.path().join("profile");
100102

101103
let tree_1 = sample_tree("foo", "foo", "a");
102104
let tree_2 = sample_tree("bar", "bar", "b");
103105

104106
(NixEnv {
105-
nix_store_path: Path::new("/nix/var/nix/profiles/default/"),
106-
nss_ca_cert_path: Path::new("/nix/var/nix/profiles/default/"),
107+
nix_store_path: &nix_store_path,
108+
nss_ca_cert_path: &nix_store_path,
107109
profile: &profile_path,
108110
pkgs: &[&tree_1, &tree_2],
109111
})
@@ -123,8 +125,8 @@ fn test_no_intersection() {
123125
let tree_4 = sample_tree("tux", "tux", "d");
124126

125127
(NixEnv {
126-
nix_store_path: Path::new("/nix/var/nix/profiles/default/"),
127-
nss_ca_cert_path: Path::new("/nix/var/nix/profiles/default/"),
128+
nix_store_path: &nix_store_path,
129+
nss_ca_cert_path: &nix_store_path,
128130
profile: &profile_path,
129131
pkgs: &[&tree_3, &tree_4],
130132
})
@@ -147,14 +149,15 @@ fn test_overlap_replaces() {
147149
return;
148150
}
149151

152+
let nix_store_path = super::super::find_nix_store_path();
150153
let profile = tempfile::tempdir().unwrap();
151154
let profile_path = profile.path().join("profile");
152155

153156
let tree_base = sample_tree("fizz", "fizz", "fizz");
154157
let tree_1 = sample_tree("foo", "foo", "a");
155158
(NixEnv {
156-
nix_store_path: Path::new("/nix/var/nix/profiles/default/"),
157-
nss_ca_cert_path: Path::new("/nix/var/nix/profiles/default/"),
159+
nix_store_path: &nix_store_path,
160+
nss_ca_cert_path: &nix_store_path,
158161
profile: &profile_path,
159162
pkgs: &[&tree_base, &tree_1],
160163
})
@@ -172,8 +175,8 @@ fn test_overlap_replaces() {
172175

173176
let tree_2 = sample_tree("foo", "foo", "b");
174177
(NixEnv {
175-
nix_store_path: Path::new("/nix/var/nix/profiles/default/"),
176-
nss_ca_cert_path: Path::new("/nix/var/nix/profiles/default/"),
178+
nix_store_path: &nix_store_path,
179+
nss_ca_cert_path: &nix_store_path,
177180
profile: &profile_path,
178181
pkgs: &[&tree_2],
179182
})
@@ -187,8 +190,8 @@ fn test_overlap_replaces() {
187190

188191
let tree_3 = sample_tree("bar", "foo", "c");
189192
(NixEnv {
190-
nix_store_path: Path::new("/nix/var/nix/profiles/default/"),
191-
nss_ca_cert_path: Path::new("/nix/var/nix/profiles/default/"),
193+
nix_store_path: &nix_store_path,
194+
nss_ca_cert_path: &nix_store_path,
192195
profile: &profile_path,
193196
pkgs: &[&tree_3],
194197
})

src/profile/nixprofile/tests.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,16 @@ fn test_detect_intersection() {
7373
return;
7474
}
7575

76+
let nix_store_path = super::super::find_nix_store_path();
7677
let profile = tempfile::tempdir().unwrap();
7778
let profile_path = profile.path().join("profile");
7879

7980
let tree_1 = sample_tree("foo", "foo", "a");
8081
let tree_2 = sample_tree("bar", "foo", "b");
8182

8283
(NixProfile {
83-
nix_store_path: Path::new("/nix/var/nix/profiles/default/"),
84-
nss_ca_cert_path: Path::new("/nix/var/nix/profiles/default/"),
84+
nix_store_path: &nix_store_path,
85+
nss_ca_cert_path: &nix_store_path,
8586
profile: &profile_path,
8687
pkgs: &[&tree_1, &tree_2],
8788
})
@@ -95,15 +96,16 @@ fn test_no_intersection() {
9596
return;
9697
}
9798

99+
let nix_store_path = super::super::find_nix_store_path();
98100
let profile = tempfile::tempdir().unwrap();
99101
let profile_path = profile.path().join("profile");
100102

101103
let tree_1 = sample_tree("foo", "foo", "a");
102104
let tree_2 = sample_tree("bar", "bar", "b");
103105

104106
(NixProfile {
105-
nix_store_path: Path::new("/nix/var/nix/profiles/default/"),
106-
nss_ca_cert_path: Path::new("/nix/var/nix/profiles/default/"),
107+
nix_store_path: &nix_store_path,
108+
nss_ca_cert_path: &nix_store_path,
107109
profile: &profile_path,
108110
pkgs: &[&tree_1, &tree_2],
109111
})
@@ -123,8 +125,8 @@ fn test_no_intersection() {
123125
let tree_4 = sample_tree("tux", "tux", "d");
124126

125127
(NixProfile {
126-
nix_store_path: Path::new("/nix/var/nix/profiles/default/"),
127-
nss_ca_cert_path: Path::new("/nix/var/nix/profiles/default/"),
128+
nix_store_path: &nix_store_path,
129+
nss_ca_cert_path: &nix_store_path,
128130
profile: &profile_path,
129131
pkgs: &[&tree_3, &tree_4],
130132
})
@@ -147,14 +149,15 @@ fn test_overlap_replaces() {
147149
return;
148150
}
149151

152+
let nix_store_path = super::super::find_nix_store_path();
150153
let profile = tempfile::tempdir().unwrap();
151154
let profile_path = profile.path().join("profile");
152155

153156
let tree_base = sample_tree("fizz", "fizz", "fizz");
154157
let tree_1 = sample_tree("foo", "foo", "a");
155158
(NixProfile {
156-
nix_store_path: Path::new("/nix/var/nix/profiles/default/"),
157-
nss_ca_cert_path: Path::new("/nix/var/nix/profiles/default/"),
159+
nix_store_path: &nix_store_path,
160+
nss_ca_cert_path: &nix_store_path,
158161
profile: &profile_path,
159162
pkgs: &[&tree_base, &tree_1],
160163
})
@@ -172,8 +175,8 @@ fn test_overlap_replaces() {
172175

173176
let tree_2 = sample_tree("foo", "foo", "b");
174177
(NixProfile {
175-
nix_store_path: Path::new("/nix/var/nix/profiles/default/"),
176-
nss_ca_cert_path: Path::new("/nix/var/nix/profiles/default/"),
178+
nix_store_path: &nix_store_path,
179+
nss_ca_cert_path: &nix_store_path,
177180
profile: &profile_path,
178181
pkgs: &[&tree_2],
179182
})
@@ -187,8 +190,8 @@ fn test_overlap_replaces() {
187190

188191
let tree_3 = sample_tree("bar", "foo", "c");
189192
(NixProfile {
190-
nix_store_path: Path::new("/nix/var/nix/profiles/default/"),
191-
nss_ca_cert_path: Path::new("/nix/var/nix/profiles/default/"),
193+
nix_store_path: &nix_store_path,
194+
nss_ca_cert_path: &nix_store_path,
192195
profile: &profile_path,
193196
pkgs: &[&tree_3],
194197
})

0 commit comments

Comments
 (0)