|
| 1 | +import test from "ava"; |
| 2 | +import sinon from "sinon"; |
| 3 | + |
| 4 | +import { RepositoryPropertyName } from "../feature-flags/properties"; |
| 5 | +import { |
| 6 | + getTestActionsEnv, |
| 7 | + RecordingLogger, |
| 8 | + setupTests, |
| 9 | +} from "../testing-utils"; |
| 10 | + |
| 11 | +import { getConfigFileInput } from "./file"; |
| 12 | + |
| 13 | +setupTests(test); |
| 14 | + |
| 15 | +test("getConfigFileInput returns undefined by default", async (t) => { |
| 16 | + const logger = new RecordingLogger(); |
| 17 | + const actionsEnv = getTestActionsEnv(); |
| 18 | + const result = getConfigFileInput(logger, actionsEnv, {}); |
| 19 | + t.is(result, undefined); |
| 20 | +}); |
| 21 | + |
| 22 | +const repositoryProperties = { |
| 23 | + [RepositoryPropertyName.CONFIG_FILE]: "/path/from/property", |
| 24 | +}; |
| 25 | + |
| 26 | +test("getConfigFileInput returns input value", async (t) => { |
| 27 | + const logger = new RecordingLogger(); |
| 28 | + const actionsEnv = getTestActionsEnv(); |
| 29 | + const testInput = "/some/path"; |
| 30 | + sinon |
| 31 | + .stub(actionsEnv, "getOptionalInput") |
| 32 | + .withArgs("config-file") |
| 33 | + .returns(testInput); |
| 34 | + |
| 35 | + // Even though both an input and repository property are configured, |
| 36 | + // we prefer the direct input to the Action. |
| 37 | + const result = getConfigFileInput(logger, actionsEnv, repositoryProperties); |
| 38 | + t.is(result, testInput); |
| 39 | + |
| 40 | + // Check for the expected log message. |
| 41 | + t.true(logger.hasMessage("Using configuration file input from workflow")); |
| 42 | +}); |
| 43 | + |
| 44 | +test("getConfigFileInput returns repository property value", async (t) => { |
| 45 | + const logger = new RecordingLogger(); |
| 46 | + const actionsEnv = getTestActionsEnv(); |
| 47 | + |
| 48 | + // Since there is no direct input, we should use the repository property. |
| 49 | + const result = getConfigFileInput(logger, actionsEnv, repositoryProperties); |
| 50 | + t.is(result, repositoryProperties[RepositoryPropertyName.CONFIG_FILE]); |
| 51 | + |
| 52 | + // Check for the expected log message. |
| 53 | + t.true( |
| 54 | + logger.hasMessage( |
| 55 | + "Using configuration file input from repository property", |
| 56 | + ), |
| 57 | + ); |
| 58 | +}); |
| 59 | + |
| 60 | +test("getConfigFileInput ignores empty repository property value", async (t) => { |
| 61 | + const logger = new RecordingLogger(); |
| 62 | + const actionsEnv = getTestActionsEnv(); |
| 63 | + |
| 64 | + // Since the repository property value is an empty/whitespace string, we should ignore it. |
| 65 | + const result = getConfigFileInput(logger, actionsEnv, { |
| 66 | + [RepositoryPropertyName.CONFIG_FILE]: " ", |
| 67 | + }); |
| 68 | + t.is(result, undefined); |
| 69 | +}); |
0 commit comments