|
| 1 | +import { mkdtemp, rm, writeFile, mkdir } from "fs/promises"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 3 | +import { join } from "path"; |
| 4 | +import { tmpdir } from "os"; |
| 5 | + |
| 6 | +import { Config } from "./config"; |
| 7 | +import { loadConfigFile } from "./configFile"; |
| 8 | + |
| 9 | +interface LocalTestContext { |
| 10 | + config: Config; |
| 11 | + tempDir: string; |
| 12 | +} |
| 13 | + |
| 14 | +describe.concurrent("configFile", () => { |
| 15 | + beforeEach<LocalTestContext>(async (ctx) => { |
| 16 | + const tempDir = await mkdtemp(join(tmpdir(), "actions-sync-config")); |
| 17 | + await mkdir(join(tempDir, ".github"), { recursive: true }); |
| 18 | + |
| 19 | + ctx.tempDir = tempDir; |
| 20 | + ctx.config = { |
| 21 | + commitBranch: "gha/actions-sync", |
| 22 | + commitMessage: "chore: sync files", |
| 23 | + commitUserEmail: "test@example.com", |
| 24 | + commitUserName: "testing", |
| 25 | + configFilePath: join(tempDir, ".github/actions-sync.yml"), |
| 26 | + fullPath: tempDir, |
| 27 | + ignoreFiles: [], |
| 28 | + path: "", |
| 29 | + prAssignee: "", |
| 30 | + prBody: "", |
| 31 | + prEnabled: true, |
| 32 | + prLabels: [], |
| 33 | + prReviewUsers: [], |
| 34 | + prTitle: "chore: sync files", |
| 35 | + syncAuth: "", |
| 36 | + syncPath: "", |
| 37 | + syncRepository: "test/test", |
| 38 | + syncTree: "main", |
| 39 | + templateVariables: {}, |
| 40 | + }; |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach<LocalTestContext>(async (ctx) => { |
| 44 | + await rm(ctx.tempDir, { recursive: true }); |
| 45 | + }); |
| 46 | + |
| 47 | + it<LocalTestContext>("returns original config when no config file exists", async (ctx) => { |
| 48 | + const result = await loadConfigFile(ctx.config); |
| 49 | + expect(result).toEqual(ctx.config); |
| 50 | + }); |
| 51 | + |
| 52 | + it<LocalTestContext>("loads ignore-files from config file", async (ctx) => { |
| 53 | + await writeFile( |
| 54 | + ctx.config.configFilePath, |
| 55 | + `ignore-files: |
| 56 | + - .credo.exs |
| 57 | + - .formatter.exs |
| 58 | +`, |
| 59 | + ); |
| 60 | + |
| 61 | + const result = await loadConfigFile(ctx.config); |
| 62 | + expect(result.ignoreFiles).toEqual([".credo.exs", ".formatter.exs"]); |
| 63 | + }); |
| 64 | + |
| 65 | + it<LocalTestContext>("action input takes precedence over config file for ignore-files", async (ctx) => { |
| 66 | + await writeFile( |
| 67 | + ctx.config.configFilePath, |
| 68 | + `ignore-files: |
| 69 | + - .credo.exs |
| 70 | +`, |
| 71 | + ); |
| 72 | + |
| 73 | + const configWithInput = { |
| 74 | + ...ctx.config, |
| 75 | + ignoreFiles: [".formatter.exs"], |
| 76 | + }; |
| 77 | + |
| 78 | + const result = await loadConfigFile(configWithInput); |
| 79 | + expect(result.ignoreFiles).toEqual([".formatter.exs"]); |
| 80 | + }); |
| 81 | + |
| 82 | + it<LocalTestContext>("loads commit-branch from config file", async (ctx) => { |
| 83 | + await writeFile( |
| 84 | + ctx.config.configFilePath, |
| 85 | + `commit-branch: custom/branch |
| 86 | +`, |
| 87 | + ); |
| 88 | + |
| 89 | + // Simulate empty action input by setting to empty string |
| 90 | + const configWithEmptyInput = { |
| 91 | + ...ctx.config, |
| 92 | + commitBranch: "", |
| 93 | + }; |
| 94 | + |
| 95 | + const result = await loadConfigFile(configWithEmptyInput); |
| 96 | + expect(result.commitBranch).toBe("custom/branch"); |
| 97 | + }); |
| 98 | + |
| 99 | + it<LocalTestContext>("loads pr-enabled from config file", async (ctx) => { |
| 100 | + await writeFile( |
| 101 | + ctx.config.configFilePath, |
| 102 | + `pr-enabled: false |
| 103 | +`, |
| 104 | + ); |
| 105 | + |
| 106 | + const result = await loadConfigFile(ctx.config); |
| 107 | + expect(result.prEnabled).toBe(false); |
| 108 | + }); |
| 109 | + |
| 110 | + it<LocalTestContext>("loads pr-labels from config file", async (ctx) => { |
| 111 | + await writeFile( |
| 112 | + ctx.config.configFilePath, |
| 113 | + `pr-labels: |
| 114 | + - sync |
| 115 | + - automated |
| 116 | +`, |
| 117 | + ); |
| 118 | + |
| 119 | + const result = await loadConfigFile(ctx.config); |
| 120 | + expect(result.prLabels).toEqual(["sync", "automated"]); |
| 121 | + }); |
| 122 | + |
| 123 | + it<LocalTestContext>("action input takes precedence over config file for pr-labels", async (ctx) => { |
| 124 | + await writeFile( |
| 125 | + ctx.config.configFilePath, |
| 126 | + `pr-labels: |
| 127 | + - from-file |
| 128 | +`, |
| 129 | + ); |
| 130 | + |
| 131 | + const configWithInput = { |
| 132 | + ...ctx.config, |
| 133 | + prLabels: ["from-action"], |
| 134 | + }; |
| 135 | + |
| 136 | + const result = await loadConfigFile(configWithInput); |
| 137 | + expect(result.prLabels).toEqual(["from-action"]); |
| 138 | + }); |
| 139 | + |
| 140 | + it<LocalTestContext>("handles multiple config options together", async (ctx) => { |
| 141 | + await writeFile( |
| 142 | + ctx.config.configFilePath, |
| 143 | + `commit-branch: feature/sync |
| 144 | +pr-title: "feat: sync configuration" |
| 145 | +pr-labels: |
| 146 | + - sync |
| 147 | +ignore-files: |
| 148 | + - "*.md" |
| 149 | +`, |
| 150 | + ); |
| 151 | + |
| 152 | + const configWithEmptyInputs = { |
| 153 | + ...ctx.config, |
| 154 | + commitBranch: "", |
| 155 | + prTitle: "", |
| 156 | + }; |
| 157 | + |
| 158 | + const result = await loadConfigFile(configWithEmptyInputs); |
| 159 | + expect(result.commitBranch).toBe("feature/sync"); |
| 160 | + expect(result.prTitle).toBe("feat: sync configuration"); |
| 161 | + expect(result.prLabels).toEqual(["sync"]); |
| 162 | + expect(result.ignoreFiles).toEqual(["*.md"]); |
| 163 | + }); |
| 164 | + |
| 165 | + it<LocalTestContext>("handles invalid YAML gracefully", async (ctx) => { |
| 166 | + await writeFile(ctx.config.configFilePath, "invalid: yaml: content: ["); |
| 167 | + |
| 168 | + const result = await loadConfigFile(ctx.config); |
| 169 | + // Should return original config when YAML is invalid |
| 170 | + expect(result).toEqual(ctx.config); |
| 171 | + }); |
| 172 | +}); |
0 commit comments