|
| 1 | +/* eslint-disable no-undef */ |
| 2 | +require("should"); |
| 3 | +const fs = require("fs"); |
| 4 | +const path = require("path"); |
| 5 | +const { |
| 6 | + getTmpDir, |
| 7 | + createTmpDir, |
| 8 | + removeTmpDir, |
| 9 | + writeJsonFile, |
| 10 | +} = require("./utils"); |
| 11 | +const { patchPackageRepository } = require("../patchPackageRepository"); |
| 12 | + |
| 13 | +describe("patchPackageRepository.js", function () { |
| 14 | + const tmpDir = getTmpDir("test-patch-package-repository"); |
| 15 | + const repoUrl = "https://github.com/openupm/test-package"; |
| 16 | + const revision = "0123456789abcdef0123456789abcdef01234567"; |
| 17 | + |
| 18 | + beforeEach(function () { |
| 19 | + removeTmpDir("test-patch-package-repository"); |
| 20 | + createTmpDir("test-patch-package-repository"); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(function () { |
| 24 | + removeTmpDir("test-patch-package-repository"); |
| 25 | + }); |
| 26 | + |
| 27 | + it("adds repository metadata when missing", function () { |
| 28 | + writeJsonFile(path.resolve(tmpDir, "package.json"), { |
| 29 | + name: "package-a", |
| 30 | + version: "1.0.0", |
| 31 | + }); |
| 32 | + |
| 33 | + const result = patchPackageRepository(tmpDir, repoUrl, revision); |
| 34 | + |
| 35 | + result.repository.should.deepEqual({ |
| 36 | + type: "git", |
| 37 | + url: repoUrl, |
| 38 | + revision, |
| 39 | + }); |
| 40 | + |
| 41 | + const saved = JSON.parse( |
| 42 | + fs.readFileSync(path.resolve(tmpDir, "package.json"), "utf8"), |
| 43 | + ); |
| 44 | + saved.repository.should.deepEqual(result.repository); |
| 45 | + }); |
| 46 | + |
| 47 | + it("replaces string repository metadata", function () { |
| 48 | + writeJsonFile(path.resolve(tmpDir, "package.json"), { |
| 49 | + name: "package-a", |
| 50 | + version: "1.0.0", |
| 51 | + repository: "https://example.invalid/old.git", |
| 52 | + }); |
| 53 | + |
| 54 | + const result = patchPackageRepository(tmpDir, repoUrl, revision); |
| 55 | + |
| 56 | + result.repository.should.deepEqual({ |
| 57 | + type: "git", |
| 58 | + url: repoUrl, |
| 59 | + revision, |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it("preserves extra object repository metadata", function () { |
| 64 | + writeJsonFile(path.resolve(tmpDir, "package.json"), { |
| 65 | + name: "package-a", |
| 66 | + version: "1.0.0", |
| 67 | + repository: { |
| 68 | + type: "git", |
| 69 | + url: "https://example.invalid/old.git", |
| 70 | + directory: "Packages/package-a", |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + const result = patchPackageRepository(tmpDir, repoUrl, revision); |
| 75 | + |
| 76 | + result.repository.should.deepEqual({ |
| 77 | + type: "git", |
| 78 | + url: repoUrl, |
| 79 | + revision, |
| 80 | + directory: "Packages/package-a", |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it("preserves unrelated manifest fields", function () { |
| 85 | + writeJsonFile(path.resolve(tmpDir, "package.json"), { |
| 86 | + name: "package-a", |
| 87 | + version: "1.0.0", |
| 88 | + displayName: "Package A", |
| 89 | + dependencies: { |
| 90 | + "package-b": "2.0.0", |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + const result = patchPackageRepository(tmpDir, repoUrl, revision); |
| 95 | + |
| 96 | + result.name.should.equal("package-a"); |
| 97 | + result.version.should.equal("1.0.0"); |
| 98 | + result.displayName.should.equal("Package A"); |
| 99 | + result.dependencies.should.deepEqual({ |
| 100 | + "package-b": "2.0.0", |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it("rejects a missing package manifest", function () { |
| 105 | + (() => patchPackageRepository(tmpDir, repoUrl, revision)).should.throw( |
| 106 | + /ENOENT/, |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + it("rejects an invalid package manifest", function () { |
| 111 | + fs.writeFileSync(path.resolve(tmpDir, "package.json"), "{"); |
| 112 | + |
| 113 | + (() => patchPackageRepository(tmpDir, repoUrl, revision)).should.throw(); |
| 114 | + }); |
| 115 | +}); |
0 commit comments