Skip to content

Commit

Permalink
feat: allow options to be configured for hcl merge scaffolder actions (
Browse files Browse the repository at this point in the history
  • Loading branch information
bbckr authored Nov 20, 2024
1 parent 93fdc95 commit 4d9e257
Show file tree
Hide file tree
Showing 6 changed files with 623 additions and 30 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['>=20.8.0']
node-version: [20.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -34,7 +34,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['>=20.8.0']
node-version: [20.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -48,7 +48,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['>=20.8.0']
node-version: [20.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -65,7 +65,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '>=20.8.0'
node-version: '20.x'
cache: 'yarn'
# failures with peer dependencies will fail release
- run: yarn install
Expand All @@ -81,10 +81,10 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Use Node.js >=20.8.0
- name: Use Node.js 20.x
uses: actions/setup-node@v3
with:
node-version: '>=20.8.0'
node-version: '20.x'
cache: 'yarn'
- run: yarn install
- run: yarn tsc
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '>=20.8.0'
node-version: '20.x'
cache: 'yarn'
- run: yarn install
- run: yarn tsc
Expand Down
2 changes: 2 additions & 0 deletions plugins/scaffolder-backend-module-hcl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
"dependencies": {
"@backstage/backend-common": "^0.24.1",
"@backstage/backend-plugin-api": "^0.8.1",
"@backstage/backend-test-utils": "^1.1.0",
"@backstage/config": "^1.2.0",
"@backstage/plugin-scaffolder-node": "^0.4.10",
"@backstage/types": "^1.2.0",
"@seatgeek/node-hcl": "^1.0.0",
"fs-extra": "^11.2.0",
"zod": "^3.23.8"
Expand Down
82 changes: 77 additions & 5 deletions plugins/scaffolder-backend-module-hcl/src/actions/hcl/hcl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
* Copyright SeatGeek
* Licensed under the terms of the Apache-2.0 license. See LICENSE file in project root for terms.
*/
import { getVoidLogger } from '@backstage/backend-common';
import { mockServices } from '@backstage/backend-test-utils';
import { randomBytes } from 'crypto';
import { writeFileSync } from 'fs-extra';
import { tmpdir } from 'os';
import { PassThrough } from 'stream';
import { createHclMergeAction, createHclMergeFilesAction } from './hcl';

// Since we have to
const TMP_DIR = tmpdir();

beforeEach(() => {
jest.clearAllMocks();
});

describe('createHclMergeAction', () => {
const mockContext = {
logger: getVoidLogger(),
logger: mockServices.logger.mock(),
logStream: new PassThrough(),
output: jest.fn(),
createTemporaryDirectory: jest.fn(),
Expand All @@ -29,18 +32,79 @@ describe('createHclMergeAction', () => {
description = "Name to be used on all the resources as identifier"
type = string
default = ""
map = {
foo = "bar"
}
}`;

const b = `
variable "name" {
type = string
default = "my-name"
map = {
bar = "baz"
}
}`;

// with mergeMapKeys set to false, the map will be overridden
const expected = `variable "name" {
default = "my-name"
description = "Name to be used on all the resources as identifier"
type = string
map = {
bar = "baz"
}
type = string
}
`;

const mockCtx = {
...mockContext,
input: {
aSourceContent: a,
bSourceContent: b,
options: {
mergeMapKeys: false,
},
},
};

// @ts-expect-error
await createHclMergeAction().handler(mockCtx);

expect(mockCtx.output.mock.calls[0][0]).toEqual('hcl');
expect(mockCtx.output.mock.calls[0][1]).toEqual(expected);
});

it('should merge HCL files with mergeMapKeys set to true', async () => {
const a = `
variable "name" {
type = string
map1 = {
foo = "bar"
}
}`;

const b = `
variable "name" {
default = "my-name"
map1 = {
bar = "baz"
}
}`;

// with mergeMapKeys set to true, the map will be merged
const expected = `variable "name" {
default = "my-name"
map1 = {
bar = "baz"
foo = "bar"
}
type = string
}
`;
Expand All @@ -50,9 +114,13 @@ describe('createHclMergeAction', () => {
input: {
aSourceContent: a,
bSourceContent: b,
options: {
mergeMapKeys: true,
},
},
};

// @ts-expect-error
await createHclMergeAction().handler(mockCtx);

expect(mockCtx.output.mock.calls[0][0]).toEqual('hcl');
Expand All @@ -62,7 +130,7 @@ describe('createHclMergeAction', () => {

describe('createHclMergeFilesAction', () => {
const mockContext = {
logger: getVoidLogger(),
logger: mockServices.logger.mock(),
logStream: new PassThrough(),
output: jest.fn(),
createTemporaryDirectory: jest.fn(),
Expand Down Expand Up @@ -108,9 +176,13 @@ describe('createHclMergeFilesAction', () => {
input: {
aSourcePath: aPath,
bSourcePath: bPath,
options: {
mergeMapKeys: false,
},
},
};

// @ts-expect-error
await createHclMergeFilesAction().handler(mockCtx);

expect(mockCtx.output.mock.calls[0][0]).toEqual('hcl');
Expand Down
Loading

0 comments on commit 4d9e257

Please sign in to comment.