Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make pulumi-version-file also work in non-install mode #1218

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## HEAD (Unreleased)

- fix: make `pulumi-version-file` work in non-install mode as well
((#1216)[https://github.com/pulumi/actions/pull/1216])

---

## 5.3.2 (2024-06-25)
Expand Down
117 changes: 115 additions & 2 deletions src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('config.ts', () => {
});
it('should validate a configuration', async () => {
setupMockedConfig(defaultConfig);
const c = await makeConfig();
const c = makeConfig();

expect(c).toBeTruthy();
expect(c).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -81,8 +81,121 @@ describe('config.ts', () => {
command: 'invalid',
});

await expect(() => makeConfig()).toThrowErrorMatchingInlineSnapshot(
expect(() => makeConfig()).toThrowErrorMatchingInlineSnapshot(
`"Input was not correct for command. Valid alternatives are: up, update, refresh, destroy, preview, output"`,
);
});

it('should return ^3 if pulumi-version is undefined', async () => {
jest.mock('@actions/core', () => ({
getInput: jest.fn((name: string) => {
switch (name) {
case 'pulumi-version':
return undefined;
}
return defaultConfig[name];
}),
getBooleanInput: jest.fn((name: string) => {
return defaultConfig[name];
}),
getMultilineInput: jest.fn((name: string) => {
return defaultConfig[name];
}),
}));
const { makeConfig } = require('../config');
const conf = makeConfig();
expect(conf.pulumiVersion).toEqual('^3');
});

it('should read version from pulumi-version-file', async () => {
jest.mock('@actions/core', () => ({
getInput: jest.fn((name: string) => {
switch (name) {
case 'pulumi-version-file':
return '.pulumi.version';
case 'pulumi-version':
return undefined;
}
return defaultConfig[name];
}),
getBooleanInput: jest.fn((name: string) => {
return defaultConfig[name];
}),
getMultilineInput: jest.fn((name: string) => {
return defaultConfig[name];
}),
}));
jest.mock('fs', () => ({
...jest.requireActual('fs'),
readFileSync: jest.fn((path: string) => {
expect(path).toEqual('.pulumi.version');
return '3.121.0';
}),
existsSync: jest.fn(() => {
return true;
}),
}));
const { makeConfig } = require('../config');
const conf = makeConfig();
expect(conf.pulumiVersion).toEqual('3.121.0');
});

it('should fail if pulumi-version-file does not exist', async () => {
jest.mock('@actions/core', () => ({
getInput: jest.fn((name: string) => {
switch (name) {
case 'pulumi-version-file':
return '.pulumi.version';
case 'pulumi-version':
return undefined;
}
return defaultConfig[name];
}),
}));
jest.mock('fs', () => ({
...jest.requireActual('fs'),
readFileSync: jest.fn((path: string) => {
expect(path).toEqual('.pulumi.version');
return '3.121.0';
}),
existsSync: jest.fn(() => {
return false;
}),
}));
const { makeConfig } = require('../config');
expect(() => {
makeConfig();
}).toThrow(/pulumi-version-file '\.pulumi\.version' does not exist/);
});

it('should fail if pulumi-version-file and pulumi-version are both provided', async () => {
jest.mock('@actions/core', () => ({
getInput: jest.fn((name: string) => {
switch (name) {
case 'pulumi-version-file':
return '.pulumi.version';
case 'pulumi-version':
return '^3';
}
return defaultConfig[name];
}),
}));
jest.mock('fs', () => ({
...jest.requireActual('fs'),
readFileSync: jest.fn((path: string) => {
expect(path).toEqual('.pulumi.version');
return '3.121.0';
}),
existsSync: jest.fn(() => {
return false;
}),
}));
const { makeConfig } = require('../config');
expect(() => {
makeConfig();
}).toThrow(
/Only one of 'pulumi-version' or 'pulumi-version-file' should be provided, got both/,
);
});

});
18 changes: 17 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ export function makeInstallationConfig(): rt.Result<InstallationConfig> {

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function makeConfig() {
let pulumiVersion = getInput('pulumi-version');
const versionFile = getInput('pulumi-version-file');
if (pulumiVersion && versionFile) {
throw new Error(
"Only one of 'pulumi-version' or 'pulumi-version-file' should be provided, got both",
);
}
if (versionFile) {
if (fs.existsSync(versionFile)) {
pulumiVersion = fs
.readFileSync(versionFile, { encoding: 'utf-8' })
.trim();
} else {
throw new Error(`pulumi-version-file '${versionFile}' does not exist`);
}
}
return {
command: getUnionInput('command', {
required: true,
Expand All @@ -60,7 +76,7 @@ export function makeConfig() {
] as const,
}),
stackName: getInput('stack-name', { required: true }),
pulumiVersion: getInput('pulumi-version') ?? '^3',
pulumiVersion: pulumiVersion ?? '^3',
workDir: getInput('work-dir', { required: true }),
secretsProvider: getInput('secrets-provider'),
cloudUrl: getInput('cloud-url'),
Expand Down
Loading