|
| 1 | +/* |
| 2 | + * Copyright SeatGeek |
| 3 | + * Licensed under the terms of the Apache-2.0 license. See LICENSE file in project root for terms. |
| 4 | + */ |
| 5 | +import { getVoidLogger } from '@backstage/backend-common'; |
| 6 | +import { randomBytes } from 'crypto'; |
| 7 | +import { writeFileSync } from 'fs-extra'; |
| 8 | +import { tmpdir } from 'os'; |
| 9 | +import { PassThrough } from 'stream'; |
| 10 | +import { createHclMergeAction, createHclMergeFilesAction } from './hcl'; |
| 11 | + |
| 12 | +// Since we have to |
| 13 | +const TMP_DIR = tmpdir(); |
| 14 | + |
| 15 | +describe('createHclMergeAction', () => { |
| 16 | + const mockContext = { |
| 17 | + logger: getVoidLogger(), |
| 18 | + logStream: new PassThrough(), |
| 19 | + output: jest.fn(), |
| 20 | + createTemporaryDirectory: jest.fn(), |
| 21 | + checkpoint: jest.fn(), |
| 22 | + getInitiatorCredentials: jest.fn(), |
| 23 | + workspacePath: '.', |
| 24 | + }; |
| 25 | + |
| 26 | + it('should merge HCL files', async () => { |
| 27 | + const a = ` |
| 28 | + variable "name" { |
| 29 | + description = "Name to be used on all the resources as identifier" |
| 30 | + type = string |
| 31 | + default = "" |
| 32 | + }`; |
| 33 | + |
| 34 | + const b = ` |
| 35 | + variable "name" { |
| 36 | + type = string |
| 37 | + default = "my-name" |
| 38 | + }`; |
| 39 | + |
| 40 | + const expected = `variable "name" { |
| 41 | + default = "my-name" |
| 42 | + description = "Name to be used on all the resources as identifier" |
| 43 | + type = string |
| 44 | +} |
| 45 | +
|
| 46 | +`; |
| 47 | + |
| 48 | + const mockCtx = { |
| 49 | + ...mockContext, |
| 50 | + input: { |
| 51 | + aSourceContent: a, |
| 52 | + bSourceContent: b, |
| 53 | + }, |
| 54 | + }; |
| 55 | + |
| 56 | + await createHclMergeAction().handler(mockCtx); |
| 57 | + |
| 58 | + expect(mockCtx.output.mock.calls[0][0]).toEqual('hcl'); |
| 59 | + expect(mockCtx.output.mock.calls[0][1]).toEqual(expected); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('createHclMergeFilesAction', () => { |
| 64 | + const mockContext = { |
| 65 | + logger: getVoidLogger(), |
| 66 | + logStream: new PassThrough(), |
| 67 | + output: jest.fn(), |
| 68 | + createTemporaryDirectory: jest.fn(), |
| 69 | + checkpoint: jest.fn(), |
| 70 | + getInitiatorCredentials: jest.fn(), |
| 71 | + workspacePath: TMP_DIR, |
| 72 | + }; |
| 73 | + |
| 74 | + it('should merge HCL files', async () => { |
| 75 | + const a = ` |
| 76 | + variable "name" { |
| 77 | + description = "Name to be used on all the resources as identifier" |
| 78 | + type = string |
| 79 | + default = "" |
| 80 | + }`; |
| 81 | + |
| 82 | + const b = ` |
| 83 | + variable "name" { |
| 84 | + type = string |
| 85 | + default = "my-name" |
| 86 | + }`; |
| 87 | + |
| 88 | + const expected = `variable "name" { |
| 89 | + default = "my-name" |
| 90 | + description = "Name to be used on all the resources as identifier" |
| 91 | + type = string |
| 92 | +} |
| 93 | +
|
| 94 | +`; |
| 95 | + |
| 96 | + const aPath = `${mockContext.workspacePath}/${randomBytes(12).toString( |
| 97 | + 'hex', |
| 98 | + )}.hcl`; |
| 99 | + await writeFileSync(aPath, a, 'utf8'); |
| 100 | + |
| 101 | + const bPath = `${mockContext.workspacePath}/${randomBytes(12).toString( |
| 102 | + 'hex', |
| 103 | + )}.hcl`; |
| 104 | + await writeFileSync(bPath, b, 'utf8'); |
| 105 | + |
| 106 | + const mockCtx = { |
| 107 | + ...mockContext, |
| 108 | + input: { |
| 109 | + aSourcePath: aPath, |
| 110 | + bSourcePath: bPath, |
| 111 | + }, |
| 112 | + }; |
| 113 | + |
| 114 | + await createHclMergeFilesAction().handler(mockCtx); |
| 115 | + |
| 116 | + expect(mockCtx.output.mock.calls[0][0]).toEqual('hcl'); |
| 117 | + expect(mockCtx.output.mock.calls[0][1]).toEqual(expected); |
| 118 | + }); |
| 119 | +}); |
0 commit comments