| 1 | + | package nix |
| 2 | + | |
| 3 | + | import ( |
| 4 | + | "path" |
| 5 | + | "testing" |
| 6 | + | |
| 7 | + | "github.com/stretchr/testify/assert" |
| 8 | + | ) |
| 9 | + | |
| 10 | + | func Test_findVersionIsh(t *testing.T) { |
| 11 | + | // note: only the package version fields are tested here, the name is tested in parseNixStorePath below. |
| 12 | + | tests := []struct { |
| 13 | + | name string |
| 14 | + | input string |
| 15 | + | wantIdx int |
| 16 | + | wantVersion string |
| 17 | + | wantPreRelease string |
| 18 | + | }{ |
| 19 | + | { |
| 20 | + | name: "no version", |
| 21 | + | input: "5q7vxm9lc4b9hifc3br4sr8dy7f2h0qa-source", |
| 22 | + | wantIdx: -1, |
| 23 | + | wantVersion: "", |
| 24 | + | wantPreRelease: "", |
| 25 | + | }, |
| 26 | + | { |
| 27 | + | name: "semver with overbite into output", |
| 28 | + | input: "/nix/store/h0cnbmfcn93xm5dg2x27ixhag1cwndga-glibc-2.34-210-bin", |
| 29 | + | wantIdx: 50, |
| 30 | + | wantVersion: "2.34-210-bin", |
| 31 | + | wantPreRelease: "210-bin", |
| 32 | + | }, |
| 33 | + | { |
| 34 | + | name: "multiple versions", |
| 35 | + | input: "5zzrvdmlkc5rh3k5862krd3wfb3pqhyf-perl5.34.1-TimeDate-2.33", |
| 36 | + | wantIdx: 53, |
| 37 | + | wantVersion: "2.33", |
| 38 | + | wantPreRelease: "", |
| 39 | + | }, |
| 40 | + | { |
| 41 | + | name: "name ends with number", |
| 42 | + | input: "55nswyz8335lk954y1ccx6as2jbq1z8f-libfido2-1.10.0", |
| 43 | + | wantIdx: 42, |
| 44 | + | wantVersion: "1.10.0", |
| 45 | + | wantPreRelease: "", |
| 46 | + | }, |
| 47 | + | { |
| 48 | + | name: "major-minor only", |
| 49 | + | input: "q8gnp7r8475p52k9gmdzsrcddw5hirbn-gdbm-1.23", |
| 50 | + | wantIdx: 38, |
| 51 | + | wantVersion: "1.23", |
| 52 | + | wantPreRelease: "", |
| 53 | + | }, |
| 54 | + | { |
| 55 | + | name: "0-prefixed version field", |
| 56 | + | input: "r705jm2icczpnmfccby3fzfrckfjakx3-perl5.34.1-URI-5.05", |
| 57 | + | wantIdx: 48, |
| 58 | + | wantVersion: "5.05", |
| 59 | + | wantPreRelease: "", |
| 60 | + | }, |
| 61 | + | { |
| 62 | + | name: "prerelease with alpha prefix", |
| 63 | + | input: "v48s6iddb518j9lc1pk3rcn3x8c2ff0j-bash-interactive-5.1-p16", |
| 64 | + | wantIdx: 50, |
| 65 | + | wantVersion: "5.1-p16", |
| 66 | + | wantPreRelease: "p16", |
| 67 | + | }, |
| 68 | + | { |
| 69 | + | |
| 70 | + | name: "0-major version", |
| 71 | + | input: "x2f9x5q6qrs6cssx09ylxqyg9q2isi1z-aws-c-http-0.6.15", |
| 72 | + | wantIdx: 44, |
| 73 | + | wantVersion: "0.6.15", |
| 74 | + | wantPreRelease: "", |
| 75 | + | }, |
| 76 | + | { |
| 77 | + | |
| 78 | + | name: "several version fields", |
| 79 | + | // note: this package version is fictitious |
| 80 | + | input: "z24qs6f5d1mmwdp73n1jfc3swj4v2c5s-krb5-1.19.3.9.10", |
| 81 | + | wantIdx: 38, |
| 82 | + | wantVersion: "1.19.3.9.10", |
| 83 | + | wantPreRelease: "", |
| 84 | + | }, |
| 85 | + | { |
| 86 | + | |
| 87 | + | name: "skip drv + major only version", |
| 88 | + | input: "z0fqylhisz47krxv8fd0izm1i2qbswfr-readline63-006.drv", |
| 89 | + | wantIdx: 44, |
| 90 | + | wantVersion: "006", |
| 91 | + | wantPreRelease: "", |
| 92 | + | }, |
| 93 | + | { |
| 94 | + | |
| 95 | + | name: "prerelease with multiple dashes", |
| 96 | + | input: "zkgyp2vra0bgqm0dv1qi514l5fd0aksx-bash-interactive-5.1-p16-man", |
| 97 | + | wantIdx: 50, |
| 98 | + | wantVersion: "5.1-p16-man", |
| 99 | + | wantPreRelease: "p16-man", |
| 100 | + | }, |
| 101 | + | { |
| 102 | + | |
| 103 | + | name: "date as major version", |
| 104 | + | input: "0amf0d1dymv9gqcyhhjb9j0l8sn00c56-libedit-20210910-3.1", |
| 105 | + | wantIdx: 41, |
| 106 | + | wantVersion: "20210910-3.1", |
| 107 | + | wantPreRelease: "3.1", |
| 108 | + | }, |
| 109 | + | { |
| 110 | + | |
| 111 | + | name: "long name", |
| 112 | + | input: "0296qxvn30z9b2ah1g5p97k5wr9k8y78-busybox-static-x86_64-unknown-linux-musl-1.35.0", |
| 113 | + | wantIdx: 74, |
| 114 | + | wantVersion: "1.35.0", |
| 115 | + | wantPreRelease: "", |
| 116 | + | }, |
| 117 | + | { |
| 118 | + | // this accounts for https://nixos.org/manual/nixpkgs/stable/#sec-package-naming |
| 119 | + | // > If a package is not a release but a commit from a repository, then the version attribute must |
| 120 | + | // > be the date of that (fetched) commit. The date must be in "unstable-YYYY-MM-DD" format. |
| 121 | + | // example: https://github.com/NixOS/nixpkgs/blob/798e23beab9b5cba4d6f05e8b243e1d4535770f3/pkgs/servers/webdav-server-rs/default.nix#L14 |
| 122 | + | name: "unstable version", |
| 123 | + | input: "q5dhwzcn82by5ndc7g0q83wsnn13qkqw-webdav-server-rs-unstable-2021-08-16", |
| 124 | + | wantIdx: 50, |
| 125 | + | wantVersion: "unstable-2021-08-16", |
| 126 | + | wantPreRelease: "", |
| 127 | + | }, |
| 128 | + | } |
| 129 | + | for _, tt := range tests { |
| 130 | + | t.Run(tt.name, func(t *testing.T) { |
| 131 | + | gotIdx, gotVersion, gotPreRelease := findVersionIsh(tt.input) |
| 132 | + | assert.Equal(t, tt.wantIdx, gotIdx) |
| 133 | + | assert.Equal(t, tt.wantVersion, gotVersion) |
| 134 | + | assert.Equal(t, tt.wantPreRelease, gotPreRelease) |
| 135 | + | }) |
| 136 | + | } |
| 137 | + | } |
| 138 | + | |
| 139 | + | func Test_parseNixStorePath(t *testing.T) { |
| 140 | + | |
| 141 | + | tests := []struct { |
| 142 | + | source string |
| 143 | + | want *nixStorePath |
| 144 | + | }{ |
| 145 | + | { |
| 146 | + | source: "/nix/store/h0cnbmfcn93xm5dg2x27ixhag1cwndga-glibc-2.34-210-bin", |
| 147 | + | want: &nixStorePath{ |
| 148 | + | outputHash: "h0cnbmfcn93xm5dg2x27ixhag1cwndga", |
| 149 | + | name: "glibc", |
| 150 | + | version: "2.34-210", |
| 151 | + | output: "bin", |
| 152 | + | }, |
| 153 | + | }, |
| 154 | + | { |
| 155 | + | source: "/nix/store/0296qxvn30z9b2ah1g5p97k5wr9k8y78-busybox-static-x86_64-unknown-linux-musl-1.35.0", |
| 156 | + | want: &nixStorePath{ |
| 157 | + | outputHash: "0296qxvn30z9b2ah1g5p97k5wr9k8y78", |
| 158 | + | name: "busybox-static-x86_64-unknown-linux-musl", |
| 159 | + | version: "1.35.0", |
| 160 | + | }, |
| 161 | + | }, |
| 162 | + | { |
| 163 | + | source: "/nix/store/5zzrvdmlkc5rh3k5862krd3wfb3pqhyf-perl5.34.1-TimeDate-2.33", |
| 164 | + | want: &nixStorePath{ |
| 165 | + | outputHash: "5zzrvdmlkc5rh3k5862krd3wfb3pqhyf", |
| 166 | + | name: "perl5.34.1-TimeDate", |
| 167 | + | version: "2.33", |
| 168 | + | }, |
| 169 | + | }, |
| 170 | + | { |
| 171 | + | source: "/nix/store/q38q8ng57zwjg1h15ry5zx0lb0xyax4b-libcap-2.63-lib", |
| 172 | + | want: &nixStorePath{ |
| 173 | + | outputHash: "q38q8ng57zwjg1h15ry5zx0lb0xyax4b", |
| 174 | + | name: "libcap", |
| 175 | + | version: "2.63", |
| 176 | + | output: "lib", |
| 177 | + | }, |
| 178 | + | }, |
| 179 | + | { |
| 180 | + | source: "/nix/store/p0y8fbpbqr2jm5zfrdll0rgyg2lvp5g2-util-linux-minimal-2.37.4-bin", |
| 181 | + | want: &nixStorePath{ |
| 182 | + | outputHash: "p0y8fbpbqr2jm5zfrdll0rgyg2lvp5g2", |
| 183 | + | name: "util-linux-minimal", |
| 184 | + | version: "2.37.4", |
| 185 | + | output: "bin", |
| 186 | + | }, |
| 187 | + | }, |
| 188 | + | { |
| 189 | + | source: "/nix/store/z24qs6f5d1mmwdp73n1jfc3swj4v2c5s-krb5-1.19.3.9.10", |
| 190 | + | want: &nixStorePath{ |
| 191 | + | outputHash: "z24qs6f5d1mmwdp73n1jfc3swj4v2c5s", |
| 192 | + | name: "krb5", |
| 193 | + | version: "1.19.3.9.10", |
| 194 | + | }, |
| 195 | + | }, |
| 196 | + | { |
| 197 | + | source: "/nix/store/zkgyp2vra0bgqm0dv1qi514l5fd0aksx-bash-interactive-5.1-p16-man", |
| 198 | + | want: &nixStorePath{ |
| 199 | + | outputHash: "zkgyp2vra0bgqm0dv1qi514l5fd0aksx", |
| 200 | + | name: "bash-interactive", |
| 201 | + | version: "5.1-p16", |
| 202 | + | output: "man", |
| 203 | + | }, |
| 204 | + | }, |
| 205 | + | { |
| 206 | + | source: "/nix/store/nwf2y0nc48ybim56308cr5ccvwkabcqc-openssl-1.1.1q", |
| 207 | + | want: &nixStorePath{ |
| 208 | + | outputHash: "nwf2y0nc48ybim56308cr5ccvwkabcqc", |
| 209 | + | name: "openssl", |
| 210 | + | version: "1.1.1q", |
| 211 | + | }, |
| 212 | + | }, |
| 213 | + | { |
| 214 | + | source: "/nix/store/nwv742f1bxv6g78hy9yc6slxdbxlmqhb-kmod-29", |
| 215 | + | want: &nixStorePath{ |
| 216 | + | outputHash: "nwv742f1bxv6g78hy9yc6slxdbxlmqhb", |
| 217 | + | name: "kmod", |
| 218 | + | version: "29", |
| 219 | + | }, |
| 220 | + | }, |
| 221 | + | { |
| 222 | + | source: "/nix/store/n83qx7m848kg51lcjchwbkmlgdaxfckf-tzdata-2022a", |
| 223 | + | want: &nixStorePath{ |
| 224 | + | outputHash: "n83qx7m848kg51lcjchwbkmlgdaxfckf", |
| 225 | + | name: "tzdata", |
| 226 | + | version: "2022a", |
| 227 | + | }, |
| 228 | + | }, |
| 229 | + | { |
| 230 | + | source: "'/nix/store/q5dhwzcn82by5ndc7g0q83wsnn13qkqw-webdav-server-rs-unstable-2021-08-16", |
| 231 | + | want: &nixStorePath{ |
| 232 | + | outputHash: "q5dhwzcn82by5ndc7g0q83wsnn13qkqw", |
| 233 | + | name: "webdav-server-rs", |
| 234 | + | version: "unstable-2021-08-16", |
| 235 | + | }, |
| 236 | + | }, |
| 237 | + | // negative cases... |
| 238 | + | { |
| 239 | + | source: "'z33yk02rsr6b4rb56lgb80bnvxx6yw39-?id=21ee35dde73aec5eba35290587d479218c6dd824.drv'", |
| 240 | + | }, |
| 241 | + | { |
| 242 | + | source: "/nix/store/yzahni8aig6mdrvcsccgwm2515lcpi5q-git-minimal-2.36.0.drv", |
| 243 | + | }, |
| 244 | + | { |
| 245 | + | source: "/nix/store/z9yvxs0s3xdkp5jgmzis4g50bfq3dgvm-0018-pkg-config-derive-prefix-from-prefix.patch", |
| 246 | + | }, |
| 247 | + | { |
| 248 | + | source: "/nix/store/w3hl7zrmc9qvzadc0k7cp9ysxiyz88j6-base-system", |
| 249 | + | }, |
| 250 | + | { |
| 251 | + | source: "/nix/store/zz1lc28x25fcx6al6xwk3dk8kp7wx47y-Test-RequiresInternet-0.05.tar.gz.drv", |
| 252 | + | }, |
| 253 | + | } |
| 254 | + | for _, tt := range tests { |
| 255 | + | t.Run(path.Base(tt.source), func(t *testing.T) { |
| 256 | + | assert.Equal(t, tt.want, parseNixStorePath(tt.source)) |
| 257 | + | }) |
| 258 | + | } |
| 259 | + | } |
| 260 | + | |
| 261 | + | func Test_parentNixStorePath(t *testing.T) { |
| 262 | + | |
| 263 | + | tests := []struct { |
| 264 | + | name string |
| 265 | + | source string |
| 266 | + | want string |
| 267 | + | }{ |
| 268 | + | { |
| 269 | + | name: "exact path from absolute root", |
| 270 | + | source: "/nix/store/5zzrvdmlkc5rh3k5862krd3wfb3pqhyf-perl5.34.1-TimeDate-2.33", |
| 271 | + | want: "", |
| 272 | + | }, |
| 273 | + | { |
| 274 | + | name: "exact path from relative root", |
| 275 | + | source: "nix/store/5zzrvdmlkc5rh3k5862krd3wfb3pqhyf-perl5.34.1-TimeDate-2.33", |
| 276 | + | want: "", |
| 277 | + | }, |
| 278 | + | { |
| 279 | + | name: "clean paths", |
| 280 | + | source: "//nix/store/5zzrvdmlkc5rh3k5862krd3wfb3pqhyf-perl5.34.1-TimeDate-2.33///", |
| 281 | + | want: "", |
| 282 | + | }, |
| 283 | + | { |
| 284 | + | name: "relative root with subdir file", |
| 285 | + | source: "nix/store/5zzrvdmlkc5rh3k5862krd3wfb3pqhyf-perl5.34.1-TimeDate-2.33/bin/perl-timedate", |
| 286 | + | want: "nix/store/5zzrvdmlkc5rh3k5862krd3wfb3pqhyf-perl5.34.1-TimeDate-2.33", |
| 287 | + | }, |
| 288 | + | { |
| 289 | + | name: "absolute root with with subdir file", |
| 290 | + | source: "/nix/store/5zzrvdmlkc5rh3k5862krd3wfb3pqhyf-perl5.34.1-TimeDate-2.33/bin/perl-timedate", |
| 291 | + | want: "/nix/store/5zzrvdmlkc5rh3k5862krd3wfb3pqhyf-perl5.34.1-TimeDate-2.33", |
| 292 | + | }, |
| 293 | + | { |
| 294 | + | name: "nexted root with with subdir file", |
| 295 | + | source: "/somewhere/nix/store/5zzrvdmlkc5rh3k5862krd3wfb3pqhyf-perl5.34.1-TimeDate-2.33/bin/perl-timedate", |
| 296 | + | want: "/somewhere/nix/store/5zzrvdmlkc5rh3k5862krd3wfb3pqhyf-perl5.34.1-TimeDate-2.33", |
| 297 | + | }, |
| 298 | + | } |
| 299 | + | for _, tt := range tests { |
| 300 | + | t.Run(tt.name, func(t *testing.T) { |
| 301 | + | assert.Equal(t, tt.want, findParentNixStorePath(tt.source)) |
| 302 | + | }) |
| 303 | + | } |
| 304 | + | } |
| 305 | + | |