|
| 1 | +use ../nut/package.nu |
| 2 | +use std assert |
| 3 | + |
| 4 | +# [test] |
| 5 | +def "resolve id with normal data" [] { |
| 6 | + let result = "https://example.com/path/repo" | package resolve "module" "v0.1.0" |
| 7 | + |
| 8 | + assert equal $result { |
| 9 | + scheme: "https" |
| 10 | + host: "example.com" |
| 11 | + path: "/path/repo" |
| 12 | + fragment: "" |
| 13 | + type: "module" |
| 14 | + version: "v0.1.0" |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +# [test] |
| 19 | +def "resolve id always results in lower case domain only" [] { |
| 20 | + let result = "https://NuCo.com/path/MyRepo" | package resolve "" "" |
| 21 | + |
| 22 | + assert equal ($result | select host path) { |
| 23 | + host: "nuco.com" |
| 24 | + path: "/path/MyRepo" |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +# [test] |
| 29 | +def "resolve id removes git postfix" [] { |
| 30 | + let result = "https://example.com/path/repo.git" | package resolve "" "" |
| 31 | + |
| 32 | + assert equal ($result | select path) { |
| 33 | + path: "/path/repo" |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +# [test] |
| 38 | +def "resolve validates scheme" [] { |
| 39 | + assert equal ("https://example.com/repo" | package resolve "" "" | get scheme) "https" |
| 40 | + assert equal ("file://example.com/repo" | package resolve "" "" | get scheme) "file" |
| 41 | + assert equal (catch-error { "http://example.com/repo" | package resolve "" "" }) "Unsupported scheme: http" |
| 42 | +} |
| 43 | + |
| 44 | +# [test] |
| 45 | +def "resolve validates when unsupported urls" [] { |
| 46 | + assert equal (catch-error { "https://example.com" | package resolve "" "" }) "Empty path is unsupported" |
| 47 | + assert equal ( catch-error { "https://user:[email protected]/repo" | package resolve "" "" }) "Credentials are unsupported" |
| 48 | + assert equal (catch-error { "https://example.com:123/repo" | package resolve "" "" }) "Port is unsupported" |
| 49 | + |
| 50 | + # todo Might want to validate this |
| 51 | + assert equal ("https://invalid_domain/repo" | package resolve "" "" | get host) "invalid_domain" |
| 52 | +} |
| 53 | + |
| 54 | +# [test] |
| 55 | +def "parse with fragment" [] { |
| 56 | + let result = "https://github.com/vyadh/nut#semver" | package resolve "" "" | get fragment |
| 57 | + |
| 58 | + assert equal $result "semver" |
| 59 | +} |
| 60 | + |
| 61 | +def catch-error [job: closure]: nothing -> string { |
| 62 | + try { |
| 63 | + do $job |
| 64 | + error make { msg: "no-error" } |
| 65 | + } catch { |error| |
| 66 | + if ($error.msg == "no-error") { |
| 67 | + error make { |
| 68 | + msg: "Expected an error" |
| 69 | + label: { |
| 70 | + text: "from here" |
| 71 | + span: (metadata $job).span |
| 72 | + } |
| 73 | + } |
| 74 | + } else { |
| 75 | + $error.msg |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments