|
| 1 | +const { calculateTags } = require("../index"); |
| 2 | +const nock = require("nock"); |
| 3 | + |
| 4 | +test("No tags", async () => { |
| 5 | + nock("https://api.github.com").get("/repos/owner/repo/tags").reply(200, []); |
| 6 | + const tags = await calculateTags("TOKEN", "owner", "repo", "refs/tags/0.0.1"); |
| 7 | + expect(tags).toEqual(new Set(["0.0.1", "0.0", "0", "latest"])); |
| 8 | +}); |
| 9 | + |
| 10 | +test("Is the latest tag", async () => { |
| 11 | + nock("https://api.github.com") |
| 12 | + .get("/repos/owner/repo/tags") |
| 13 | + .reply(200, [ |
| 14 | + { |
| 15 | + name: "1.0.0", |
| 16 | + }, |
| 17 | + ]); |
| 18 | + const tags = await calculateTags("TOKEN", "owner", "repo", "refs/tags/2.0.0"); |
| 19 | + expect(tags).toEqual(new Set(["2.0.0", "2.0", "2", "latest"])); |
| 20 | +}); |
| 21 | + |
| 22 | +test("Not the latest major tag", async () => { |
| 23 | + nock("https://api.github.com") |
| 24 | + .get("/repos/owner/repo/tags") |
| 25 | + .reply(200, [ |
| 26 | + { |
| 27 | + name: "1.0.0", |
| 28 | + name: "2.0.0", |
| 29 | + }, |
| 30 | + ]); |
| 31 | + const tags = await calculateTags("TOKEN", "owner", "repo", "refs/tags/1.1.0"); |
| 32 | + expect(tags).toEqual(new Set(["1.1.0"])); |
| 33 | +}); |
| 34 | + |
| 35 | +test("Not the latest minor tag", async () => { |
| 36 | + nock("https://api.github.com") |
| 37 | + .get("/repos/owner/repo/tags") |
| 38 | + .reply(200, [ |
| 39 | + { |
| 40 | + name: "1.0.0", |
| 41 | + name: "2.0.0", |
| 42 | + name: "2.1.0", |
| 43 | + }, |
| 44 | + ]); |
| 45 | + const tags = await calculateTags("TOKEN", "owner", "repo", "refs/tags/2.0.1"); |
| 46 | + expect(tags).toEqual(new Set(["2.0.1"])); |
| 47 | +}); |
| 48 | + |
| 49 | +test("Ignore existing pre-releases", async () => { |
| 50 | + nock("https://api.github.com") |
| 51 | + .get("/repos/owner/repo/tags") |
| 52 | + .reply(200, [ |
| 53 | + { |
| 54 | + name: "1.0.0", |
| 55 | + name: "2.0.0-rc1", |
| 56 | + }, |
| 57 | + ]); |
| 58 | + const tags = await calculateTags("TOKEN", "owner", "repo", "refs/tags/1.1.0"); |
| 59 | + expect(tags).toEqual(new Set(["1.1.0", "1.1", "1", "latest"])); |
| 60 | +}); |
| 61 | + |
| 62 | +test("Pre-release tag", async () => { |
| 63 | + nock("https://api.github.com") |
| 64 | + .get("/repos/owner/repo/tags") |
| 65 | + .reply(200, [ |
| 66 | + { |
| 67 | + name: "1.0.0", |
| 68 | + }, |
| 69 | + ]); |
| 70 | + const tags = await calculateTags( |
| 71 | + "TOKEN", |
| 72 | + "owner", |
| 73 | + "repo", |
| 74 | + "refs/tags/2.0.0-rc1" |
| 75 | + ); |
| 76 | + expect(tags).toEqual(new Set(["2.0.0-rc1"])); |
| 77 | +}); |
| 78 | + |
| 79 | +test("Not a tag", async () => { |
| 80 | + await expect( |
| 81 | + calculateTags("TOKEN", "owner", "repo", "refs/heads/main") |
| 82 | + ).rejects.toEqual(new Error("Not a tag: refs/heads/main")); |
| 83 | +}); |
| 84 | + |
| 85 | +test("Invalid semver tag", async () => { |
| 86 | + await expect( |
| 87 | + calculateTags("TOKEN", "owner", "repo", "refs/tags/v1") |
| 88 | + ).rejects.toEqual(new Error("Invalid semver tag: v1")); |
| 89 | +}); |
0 commit comments