Skip to content

Commit 6d300cd

Browse files
committed
feat: add more robust openjsf test
1 parent 4dcacbb commit 6d300cd

File tree

9 files changed

+51
-146
lines changed

9 files changed

+51
-146
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
package-lock.json
33
test/fixture/output.md
4-
test/openjsf/tmp.md
4+
test/openjsf/tmp.md
5+
test/**/*.rej

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
with:
2424
persist-credentials: false
2525
- name: Start Patch
26-
uses: pkgjs/patch-my-code-of-conduct@v1.0.3
26+
uses: pkgjs/patch-my-code-of-conduct@v1.2.0
2727
with:
2828
base_url: './BASE.md'
2929
patch_file_path: './patch'
@@ -37,3 +37,20 @@ jobs:
3737
commit-message: 'doc: update Code of Conduct'
3838
title: 'doc: update Code of Conduct'
3939
```
40+
41+
## Useful Information
42+
43+
### Generate patch file
44+
45+
- Generate a `premable+template.md`
46+
- Generate a `original-expected.md` file
47+
48+
```bash
49+
diff -Naur preamble+template.md original-expected.md > patch
50+
```
51+
52+
### Running patching script
53+
54+
```
55+
patch -i patch preamble+template.md -o expected.md --no-backup-if-mismatch
56+
```

lib/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ const output_path = core.getInput('output_file_path');
99
apply_patch(base_url, template_url, patch_path, output_path)
1010
.then(() => core.setOutput('state', 'success'))
1111
.catch(error => core.setFailed(error))
12-
13-

lib/runner.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
const fs = require('node:fs')
22
const { execSync } = require('node:child_process')
3-
const { fetch } = require('undici')
3+
const { fetch } = require('undici');
4+
const { randomUUID } = require('node:crypto');
45

5-
async function apply_patch(base_url, template_url, patch_path, output_path) {
6+
async function apply_patch(base_url, template_url, patch_path, output_path, tmp_file_path) {
67
if (!fs.existsSync(base_url)) {
78
throw new Error(`base_url (${base_url}) does not exist`)
89
}
910
const base_text = fs.readFileSync(base_url, 'utf-8');
1011
const template_response = await fetch(template_url);
12+
tmp_file_path ??= `/tmp/coc-${randomUUID()}.md`;
1113

1214
if (template_response.status !== 200 && template_response.status !== 201) {
1315
throw new Error(`template response got ${template_response.status} status code. expected 200 or 201.`)
1416
}
1517
const template_text = await template_response.text();
1618

17-
const output_text = `${base_text}${template_text}`;
18-
fs.writeFileSync(output_path, output_text);
19+
// Make sure that there is a new line at the end.
20+
const output_text = `${base_text}\n${template_text.trim()}\n`;
21+
fs.writeFileSync(tmp_file_path, output_text);
1922

2023
if (!fs.existsSync(patch_path)) {
2124
throw new Error(`patch_path (${patch_path}) does not exist`)
2225
}
2326

24-
await execSync(`patch -i ${patch_path} ${output_path} -R --no-backup-if-mismatch`);
27+
await execSync(`patch -i ${patch_path} ${tmp_file_path} -o ${output_path} --no-backup-if-mismatch`);
2528

2629
const original_file = output_path + '.orig'
2730
// Delete `output.md.orig` file, if exists

test/apply.test.ts

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

test/file-operations.ts

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

test/fixture/base.md

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

test/fixture/patch.txt

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

test/openjsf.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { assert, beforeEach, test } from 'vitest';
2+
import path from 'node:path'
3+
import fs from 'node:fs'
4+
import { apply_patch } from '../lib/runner.js'
5+
6+
const template = 'https://www.contributor-covenant.org/version/2/1/code_of_conduct/code_of_conduct.md'
7+
const preamble = path.join(__dirname, './openjsf/preamble.md')
8+
const patch = path.join(__dirname, './openjsf/patch')
9+
const expected = path.join(__dirname, './openjsf/expected.md')
10+
const original_expected = path.join(__dirname, './openjsf/original-expected.md')
11+
12+
const tmp_file_path = path.join(__dirname, './openjsf/tmp.md')
13+
14+
beforeEach(() => {
15+
fs.copyFileSync(original_expected, expected)
16+
fs.rmSync(tmp_file_path, { force: true })
17+
})
18+
19+
test('should create the exact result with expected', async () => {
20+
await apply_patch(preamble, template, patch, expected, tmp_file_path);
21+
22+
assert.deepEqual(fs.readFileSync(expected, 'utf-8'), fs.readFileSync(original_expected, 'utf-8'))
23+
})

0 commit comments

Comments
 (0)