| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 |
|
| 19 | (use-modules (json)
|
| 20 | (guix build node-build-system)
|
| 21 | (guix tests)
|
| 22 | (srfi srfi-64))
|
| 23 |
|
| 24 | (define (package.json)
|
| 25 | "Sample package.json file."
|
| 26 | (call-with-output-string
|
| 27 | (lambda (out)
|
| 28 | (scm->json
|
| 29 | '(("devDependencies"
|
| 30 | ("typescript-eslint" . "^8.61.0")
|
| 31 | ("typescript" . "^5.9.3")
|
| 32 | ("typedoc" . "^0.28.19")
|
| 33 | ("htmlparser2" . "^10.1.0")
|
| 34 | ("eslint" . "^10.4.1")
|
| 35 | ("@types/node" . "^25.9.2"))
|
| 36 | ("dependencies"
|
| 37 | ("domelementtype" . "^3.0.0")
|
| 38 | ("boolbase" . ">=2.0.0"))
|
| 39 | ("peerDependencies"
|
| 40 | ("mkdirp" . ">=1.0.0")
|
| 41 | ("react" . "^16.8.0"))
|
| 42 | ("engines" ("node" . ">=20.19.0"))
|
| 43 | ("repository"
|
| 44 | ("url" . "https://example.com/dummy/dummy.git")
|
| 45 | ("type" . "git"))
|
| 46 | ("scripts"
|
| 47 | ("test" . "npm run test")
|
| 48 | ("lint" . "eslint .")
|
| 49 | ("build" . "tsc"))
|
| 50 | ("files" . #("dist" "src" "!**/*.spec.ts"))
|
| 51 | ("sideEffects" . #f)
|
| 52 | ("exports"
|
| 53 | ("." ("default" . "./dist/index.js") ("types" . "./dist/index.d.ts")))
|
| 54 | ("types" . "dist/index.d.ts")
|
| 55 | ("main" . "dist/index.js")
|
| 56 | ("license" . "GPL-3.0")
|
| 57 | ("description" . "Dummy package.json")
|
| 58 | ("version" . "6.0.1")
|
| 59 | ("name" . "dummy")
|
| 60 | ("type" . "module"))
|
| 61 | out #:pretty #t))))
|
| 62 |
|
| 63 | (define* (modify-json* #:rest all-arguments)
|
| 64 | "Wrap modify-json to use our package.json test sample and avoid file I/O.
|
| 65 | It also returns the data as an alist directly."
|
| 66 | (mock ((guix build utils) with-atomic-file-replacement
|
| 67 | (lambda (_ proc)
|
| 68 | (call-with-input-string (package.json)
|
| 69 | (lambda (in)
|
| 70 | (call-with-output-string
|
| 71 | (lambda (out)
|
| 72 | (proc in out)))))))
|
| 73 | (json-string->scm (apply modify-json all-arguments)
|
| 74 | #:ordered #t)))
|
| 75 |
|
| 76 |
|
| 77 | (test-begin "node related tests")
|
| 78 |
|
| 79 | (test-equal "modify-json, ordering"
|
| 80 | '(("mkdirp" . ">=1.0.0")
|
| 81 | ("react" . "^16.8.0"))
|
| 82 | (assoc-ref (modify-json* (modify-json-fields
|
| 83 | '("peerDependencies.react")
|
| 84 | (lambda (field-path data key)
|
| 85 | data)))
|
| 86 | "peerDependencies"))
|
| 87 |
|
| 88 | (test-equal "delete-dependencies"
|
| 89 | '(("domelementtype" . "^3.0.0"))
|
| 90 | (assoc-ref (modify-json*
|
| 91 | (delete-dependencies '("boolbase")))
|
| 92 | "dependencies"))
|
| 93 |
|
| 94 | (test-equal "delete-dependencies/except"
|
| 95 | '(("boolbase" . ">=2.0.0"))
|
| 96 | (assoc-ref (modify-json*
|
| 97 | (delete-dependencies/except '("boolbase")))
|
| 98 | "dependencies"))
|
| 99 |
|
| 100 | (test-equal "delete-dev-dependencies/except"
|
| 101 | '(("typescript" . "^5.9.3")
|
| 102 | ("@types/node" . "^25.9.2"))
|
| 103 | (assoc-ref (modify-json*
|
| 104 | (delete-dev-dependencies/except
|
| 105 | '("typescript" "@types/node")))
|
| 106 | "devDependencies"))
|
| 107 |
|
| 108 | (test-end)
|