|
| 1 | +import { describe, expect, it } from "vitest" |
| 2 | + |
| 3 | +import { Config } from "../src" |
| 4 | + |
| 5 | +describe("needs", () => { |
| 6 | + it("should support needs as simple string array", () => { |
| 7 | + const cfg = new Config() |
| 8 | + |
| 9 | + cfg.job("build", { |
| 10 | + script: ["npm run build"], |
| 11 | + }) |
| 12 | + |
| 13 | + cfg.job("test", { |
| 14 | + script: ["npm test"], |
| 15 | + needs: ["build"], |
| 16 | + }) |
| 17 | + |
| 18 | + const plain = cfg.getPlainObject() |
| 19 | + |
| 20 | + expect(plain.jobs?.test?.needs).toEqual(["build"]) |
| 21 | + }) |
| 22 | + |
| 23 | + it("should support needs with job objects", () => { |
| 24 | + const cfg = new Config() |
| 25 | + |
| 26 | + cfg.job("build", { |
| 27 | + script: ["npm run build"], |
| 28 | + }) |
| 29 | + |
| 30 | + cfg.job("test", { |
| 31 | + script: ["npm test"], |
| 32 | + needs: [ |
| 33 | + { |
| 34 | + job: "build", |
| 35 | + }, |
| 36 | + ], |
| 37 | + }) |
| 38 | + |
| 39 | + const plain = cfg.getPlainObject() |
| 40 | + |
| 41 | + expect(plain.jobs?.test?.needs).toEqual([{ job: "build" }]) |
| 42 | + }) |
| 43 | + |
| 44 | + it("should support needs with artifacts property", () => { |
| 45 | + const cfg = new Config() |
| 46 | + |
| 47 | + cfg.job("build", { |
| 48 | + script: ["npm run build"], |
| 49 | + }) |
| 50 | + |
| 51 | + cfg.job("test", { |
| 52 | + script: ["npm test"], |
| 53 | + needs: [ |
| 54 | + { |
| 55 | + job: "build", |
| 56 | + artifacts: false, |
| 57 | + }, |
| 58 | + ], |
| 59 | + }) |
| 60 | + |
| 61 | + const plain = cfg.getPlainObject() |
| 62 | + |
| 63 | + expect(plain.jobs?.test?.needs).toEqual([{ job: "build", artifacts: false }]) |
| 64 | + }) |
| 65 | + |
| 66 | + it("should support needs with optional property", () => { |
| 67 | + const cfg = new Config() |
| 68 | + |
| 69 | + cfg.job("generate_version", { |
| 70 | + script: ["echo version"], |
| 71 | + }) |
| 72 | + |
| 73 | + cfg.job("unit_tests", { |
| 74 | + script: ["npm test"], |
| 75 | + }) |
| 76 | + |
| 77 | + cfg.job("deploy", { |
| 78 | + script: ["echo deploying"], |
| 79 | + needs: [ |
| 80 | + { |
| 81 | + job: "generate_version", |
| 82 | + optional: true, |
| 83 | + }, |
| 84 | + { |
| 85 | + job: "unit_tests", |
| 86 | + }, |
| 87 | + ], |
| 88 | + }) |
| 89 | + |
| 90 | + const plain = cfg.getPlainObject() |
| 91 | + |
| 92 | + expect(plain.jobs?.deploy?.needs).toEqual([ |
| 93 | + { job: "generate_version", optional: true }, |
| 94 | + { job: "unit_tests" }, |
| 95 | + ]) |
| 96 | + }) |
| 97 | + |
| 98 | + it("should support needs with both optional and artifacts", () => { |
| 99 | + const cfg = new Config() |
| 100 | + |
| 101 | + cfg.job("build", { |
| 102 | + script: ["npm run build"], |
| 103 | + }) |
| 104 | + |
| 105 | + cfg.job("deploy", { |
| 106 | + script: ["echo deploying"], |
| 107 | + needs: [ |
| 108 | + { |
| 109 | + job: "build", |
| 110 | + artifacts: true, |
| 111 | + optional: true, |
| 112 | + }, |
| 113 | + ], |
| 114 | + }) |
| 115 | + |
| 116 | + const plain = cfg.getPlainObject() |
| 117 | + |
| 118 | + expect(plain.jobs?.deploy?.needs).toEqual([ |
| 119 | + { job: "build", artifacts: true, optional: true }, |
| 120 | + ]) |
| 121 | + }) |
| 122 | + |
| 123 | + it("should support pipeline needs with optional", () => { |
| 124 | + const cfg = new Config() |
| 125 | + |
| 126 | + cfg.job("deploy", { |
| 127 | + script: ["echo deploying"], |
| 128 | + needs: { |
| 129 | + pipeline: "other-project/pipeline", |
| 130 | + optional: true, |
| 131 | + }, |
| 132 | + }) |
| 133 | + |
| 134 | + const plain = cfg.getPlainObject() |
| 135 | + |
| 136 | + expect(plain.jobs?.deploy?.needs).toEqual({ |
| 137 | + pipeline: "other-project/pipeline", |
| 138 | + optional: true, |
| 139 | + }) |
| 140 | + }) |
| 141 | +}) |
0 commit comments