Skip to content

Commit 4fcf992

Browse files
authored
Merge branch 'main' into dependabot/npm_and_yarn/brace-expansion-1.1.13
2 parents c8c52c4 + 0771e33 commit 4fcf992

10 files changed

Lines changed: 309 additions & 115 deletions

File tree

.github/workflows/onPushToMain.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ jobs:
1111
steps:
1212
- uses: actions/checkout@v4
1313
- uses: actions/setup-node@v4
14+
with:
15+
node-version: latest
16+
- run: |
17+
npm install -g corepack --force
18+
corepack enable
1419
- name: Check if version already exists
1520
id: version-check
1621
run: |

.github/workflows/onRelease.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ jobs:
1212
- uses: actions/setup-node@v4
1313
with:
1414
node-version: latest
15+
- run: |
16+
npm install -g corepack --force
17+
corepack enable
1518
- run: yarn
1619
- uses: JS-DevTools/npm-publish@19c28f1ef146469e409470805ea4279d47c3d35c
1720
with:

.github/workflows/test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ jobs:
1717
- uses: actions/setup-node@v4
1818
with:
1919
node-version: ${{ matrix.node_version }}
20-
cache: yarn
20+
- run: |
21+
npm install -g corepack --force
22+
corepack enable
2123
- run: yarn
2224
- run: yarn build
2325
- run: yarn test

src/commands/commit/index.ts

Lines changed: 84 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -43,81 +43,90 @@ export default class Commit extends Command {
4343

4444
this.log(`[tf-patch] Found module "${module}"`)
4545

46-
dir({keep: false, unsafeCleanup: true}, async (err, dirA, cleanup) => {
47-
if (err) {
48-
throw err
49-
}
50-
51-
await fse.copy(moduleDir, dirA)
52-
await fse.remove(path.join(dirA, '.git'))
53-
54-
this.log(`[tf-patch] Diffing source module and patch dir: \nA: ${dirA}\nB: ${dirB}`)
55-
// Using yarn as reference - https://github.com/yarnpkg/berry/blob/7fab4f101d1b8a98691efe14696d18ce00ecd234/packages/plugin-patch/sources/patchUtils.ts#L281-L298
56-
const gitDiff = await spawn(
57-
`git`,
58-
[
59-
`-c`,
60-
`core.safecrlf=false`,
61-
`diff`,
62-
`--src-prefix=a/`,
63-
`--dst-prefix=b/`,
64-
`--ignore-cr-at-eol`,
65-
`--full-index`,
66-
`--no-index`,
67-
`--no-renames`,
68-
`--text`,
69-
dirA,
70-
dirB,
71-
],
72-
{
73-
env: {
74-
...process.env,
75-
// #region Predictable output
76-
// These variables aim to ignore the global git config so we get predictable output
77-
// https://git-scm.com/docs/git#Documentation/git.txt-codeGITCONFIGNOSYSTEMcode
78-
GIT_CONFIG_NOSYSTEM: `1`,
79-
HOME: ``,
80-
USERPROFILE: ``,
81-
XDG_CONFIG_HOME: ``,
82-
// #endregion
83-
},
84-
},
85-
)
86-
const stderr = (await gitDiff.stderr.toArray()).join('\n')
87-
// we cannot rely on exit code, because --no-index implies --exit-code
88-
// i.e. git diff will exit with 1 if there were differences
89-
if (stderr.length > 0) {
90-
throw new Error(
91-
`Unable to diff directories. Make sure you have a recent version of 'git' available in PATH.\nThe following error was reported by 'git':\n${stderr}`,
92-
)
93-
}
94-
95-
const controller = new AbortController()
96-
const timer = setTimeout(() => {
97-
this.log('[tf-patch] Timed out creating patch')
98-
controller.abort()
99-
}, 10_000)
100-
const stdoutArr = await gitDiff.stdout.toArray({signal: controller.signal})
101-
clearTimeout(timer)
102-
103-
const stdout = stdoutArr
104-
.join('\n')
105-
.replace(new RegExp(`(a|b)(${_.escapeRegExp(`/${normalizePath(dirA)}/`)})`, `g`), `$1/`)
106-
.replace(new RegExp(`(a|b)${_.escapeRegExp(`/${normalizePath(dirB)}/`)}`, `g`), `$1/`)
107-
.replace(new RegExp(_.escapeRegExp(`${dirA}/`), `g`), ``)
108-
.replace(new RegExp(_.escapeRegExp(`${dirB}/`), `g`), ``)
109-
110-
if (!stdout.trim()) {
111-
throw new Error(`No changes detected in module "${module}"`)
112-
}
113-
114-
await fse.ensureDir(PATCHES_DIR)
115-
const patchFile = path.join(PATCHES_DIR, `${module}.patch`)
116-
await writeFile(patchFile, stdout)
117-
this.log(`[tf-patch] Created patch file: ${patchFile}`)
118-
this.log(`[tf-patch] Now you can apply the patches after "terraform init" by running "tf-patch init"`)
119-
120-
cleanup()
46+
await new Promise<void>((resolve, reject) => {
47+
dir({keep: false, unsafeCleanup: true}, async (err, dirA, cleanup) => {
48+
if (err) {
49+
reject(err)
50+
return
51+
}
52+
53+
try {
54+
await fse.copy(moduleDir, dirA)
55+
await fse.remove(path.join(dirA, '.git'))
56+
57+
this.log(`[tf-patch] Diffing source module and patch dir: \nA: ${dirA}\nB: ${dirB}`)
58+
// Using yarn as reference - https://github.com/yarnpkg/berry/blob/7fab4f101d1b8a98691efe14696d18ce00ecd234/packages/plugin-patch/sources/patchUtils.ts#L281-L298
59+
const gitDiff = await spawn(
60+
`git`,
61+
[
62+
`-c`,
63+
`core.safecrlf=false`,
64+
`diff`,
65+
`--src-prefix=a/`,
66+
`--dst-prefix=b/`,
67+
`--ignore-cr-at-eol`,
68+
`--full-index`,
69+
`--no-index`,
70+
`--no-renames`,
71+
`--text`,
72+
dirA,
73+
dirB,
74+
],
75+
{
76+
env: {
77+
...process.env,
78+
// #region Predictable output
79+
// These variables aim to ignore the global git config so we get predictable output
80+
// https://git-scm.com/docs/git#Documentation/git.txt-codeGITCONFIGNOSYSTEMcode
81+
GIT_CONFIG_NOSYSTEM: `1`,
82+
HOME: ``,
83+
USERPROFILE: ``,
84+
XDG_CONFIG_HOME: ``,
85+
// #endregion
86+
},
87+
},
88+
)
89+
const stderr = (await gitDiff.stderr.toArray()).join('\n')
90+
// we cannot rely on exit code, because --no-index implies --exit-code
91+
// i.e. git diff will exit with 1 if there were differences
92+
if (stderr.length > 0) {
93+
throw new Error(
94+
`Unable to diff directories. Make sure you have a recent version of 'git' available in PATH.\nThe following error was reported by 'git':\n${stderr}`,
95+
)
96+
}
97+
98+
const controller = new AbortController()
99+
const timer = setTimeout(() => {
100+
this.log('[tf-patch] Timed out creating patch')
101+
controller.abort()
102+
}, 10_000)
103+
const stdoutArr = await gitDiff.stdout.toArray({signal: controller.signal})
104+
clearTimeout(timer)
105+
106+
const stdout = stdoutArr
107+
.join('\n')
108+
.replace(new RegExp(`(a|b)(${_.escapeRegExp(`/${normalizePath(dirA)}/`)})`, `g`), `$1/`)
109+
.replace(new RegExp(`(a|b)${_.escapeRegExp(`/${normalizePath(dirB)}/`)}`, `g`), `$1/`)
110+
.replace(new RegExp(_.escapeRegExp(`${dirA}/`), `g`), ``)
111+
.replace(new RegExp(_.escapeRegExp(`${dirB}/`), `g`), ``)
112+
113+
if (!stdout.trim()) {
114+
throw new Error(`No changes detected in module "${module}"`)
115+
}
116+
117+
await fse.ensureDir(PATCHES_DIR)
118+
const patchFile = path.join(PATCHES_DIR, `${module}.patch`)
119+
await writeFile(patchFile, stdout)
120+
this.log(`[tf-patch] Created patch file: ${patchFile}`)
121+
this.log(`[tf-patch] Now you can apply the patches after "terraform init" by running "tf-patch init"`)
122+
123+
cleanup()
124+
resolve()
125+
} catch (error) {
126+
cleanup()
127+
reject(error)
128+
}
129+
})
121130
})
122131
}
123132
}

src/commands/patch/index.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,35 @@ intended to be editable at will.`
3737
throw new Error(`Module "${module}" not found in terraform modules directory`)
3838
}
3939

40-
dir(async (err, tmpDir) => {
41-
if (err) {
42-
throw err
43-
}
44-
45-
await writeFile(
46-
path.join(tmpDir, PATCH_DATA_FILE),
47-
JSON.stringify({
48-
module,
49-
}),
50-
)
51-
52-
const tmpModule = path.join(tmpDir, 'module')
53-
await fse.ensureDir(tmpModule)
54-
await fse.copy(moduleDir, tmpModule)
55-
await fse.remove(path.join(tmpModule, '.git'))
56-
57-
this.log(`[tf-patch] You can now edit the following folder: ${tmpModule}`)
58-
this.log(
59-
`[tf-patch] Once you are done run "tf-patch commit ${tmpModule}" and we will store a patchfile based on your changes.`,
60-
)
40+
await new Promise<void>((resolve, reject) => {
41+
dir(async (err, tmpDir) => {
42+
if (err) {
43+
reject(err)
44+
return
45+
}
46+
47+
try {
48+
await writeFile(
49+
path.join(tmpDir, PATCH_DATA_FILE),
50+
JSON.stringify({
51+
module,
52+
}),
53+
)
54+
55+
const tmpModule = path.join(tmpDir, 'module')
56+
await fse.ensureDir(tmpModule)
57+
await fse.copy(moduleDir, tmpModule)
58+
await fse.remove(path.join(tmpModule, '.git'))
59+
60+
this.log(`[tf-patch] You can now edit the following folder: ${tmpModule}`)
61+
this.log(
62+
`[tf-patch] Once you are done run "tf-patch commit ${tmpModule}" and we will store a patchfile based on your changes.`,
63+
)
64+
resolve()
65+
} catch (error) {
66+
reject(error)
67+
}
68+
})
6169
})
6270
}
6371
}

test/commands/commit/index.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {runCommand} from '@oclif/test'
2+
import {expect} from 'chai'
3+
import * as fse from 'fs-extra'
4+
import {mkdtempSync} from 'node:fs'
5+
import {readFile, writeFile} from 'node:fs/promises'
6+
import {tmpdir} from 'node:os'
7+
import path from 'node:path'
8+
9+
import {PATCHES_DIR} from '../../../src/commands/commit/index.js'
10+
import {PATCH_DATA_FILE, TERRAFORM_MODULES_DIR} from '../../../src/commands/patch/index.js'
11+
12+
describe('commit', () => {
13+
let tmpDir: string
14+
let originalCwd: string
15+
16+
before(() => {
17+
originalCwd = process.cwd()
18+
tmpDir = mkdtempSync(path.join(tmpdir(), 'tf-patch-commit-test-'))
19+
})
20+
21+
after(async () => {
22+
process.chdir(originalCwd)
23+
await fse.remove(tmpDir)
24+
})
25+
26+
it('throws an error when folder is not found', async () => {
27+
const {error} = await runCommand('commit /nonexistent-folder-xyz-abc')
28+
expect(error?.message).to.contain('not found')
29+
})
30+
31+
it('creates a patch file from a modified module', async () => {
32+
process.chdir(tmpDir)
33+
34+
const module = 'test-module'
35+
const moduleDir = path.join(tmpDir, TERRAFORM_MODULES_DIR, module)
36+
await fse.ensureDir(moduleDir)
37+
await writeFile(path.join(moduleDir, 'main.tf'), 'resource "null_resource" "original" {}\n')
38+
39+
// Create the patched folder that commit expects
40+
const patchedDir = mkdtempSync(path.join(tmpdir(), 'tf-patch-patched-'))
41+
const moduleFolder = path.join(patchedDir, 'module')
42+
await fse.ensureDir(moduleFolder)
43+
await writeFile(path.join(moduleFolder, 'main.tf'), 'resource "null_resource" "modified" {}\n')
44+
await writeFile(
45+
path.join(patchedDir, PATCH_DATA_FILE),
46+
JSON.stringify({module}),
47+
)
48+
49+
try {
50+
const {stdout} = await runCommand(`commit ${moduleFolder}`)
51+
expect(stdout).to.contain('Created patch file')
52+
const patchFile = path.join(tmpDir, PATCHES_DIR, `${module}.patch`)
53+
expect(await fse.pathExists(patchFile)).to.equal(true)
54+
const patchContent = await readFile(patchFile, 'utf8')
55+
expect(patchContent).to.contain('original')
56+
expect(patchContent).to.contain('modified')
57+
} finally {
58+
await fse.remove(patchedDir)
59+
}
60+
})
61+
})

test/commands/hello/index.test.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/commands/hello/world.test.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)