Skip to content

Commit bb97fdb

Browse files
authored
refactor(script): extract parser function in its own file for testing (#12)
* refactor(script): extract parser function in its own file for testing * test(yaml-parser): add foundations for testing yaml-parser * test(yaml-parser): add examples where the parser should modify the file * test(yaml-parser): add example where the parser should not modify the file
1 parent ac556ba commit bb97fdb

File tree

19 files changed

+545
-45
lines changed

19 files changed

+545
-45
lines changed

package-lock.json

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"description": "Add cache parameter to GitHub Actions using setup-node",
1010
"scripts": {
1111
"start": "node cli.js",
12-
"test": "node script.js"
12+
"test": "uvu tests"
1313
},
1414
"repository": "https://github.com/oscard0m/octoherd-script-add-cache-to-node-github-action",
1515
"keywords": [
@@ -23,7 +23,10 @@
2323
"prettier": "^2.3.2",
2424
"yaml": "^2.0.0-6"
2525
},
26-
"devDependencies": {},
26+
"devDependencies": {
27+
"esm": "^3.2.25",
28+
"uvu": "^0.5.1"
29+
},
2730
"release": {
2831
"branches": [
2932
"main"

script.js

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// @ts-check
22

33
import { composeCreatePullRequest } from "octokit-plugin-create-pull-request";
4-
import prettier from "prettier";
5-
import YAML from "yaml";
6-
7-
const { parseDocument } = YAML;
4+
import { getAddCacheToSetupNodeFunction } from "./utils/yaml-parser";
85

96
const BRANCH_NAME = "add-cache-to-node-workflows";
107
const PATH = ".github/workflows";
@@ -67,45 +64,7 @@ export async function script(octokit, repository, { cache = "npm" }) {
6764
const filesToEdit = {};
6865

6966
workflowFiles.forEach((element) => {
70-
filesToEdit[element.path] = ({ content, encoding }) => {
71-
const yamlDocument = parseDocument(
72-
Buffer.from(content, encoding).toString("utf-8")
73-
);
74-
const jobs = yamlDocument.get("jobs");
75-
let cacheAdded = false;
76-
77-
for (const { value: job } of jobs.items) {
78-
const steps = job.get("steps");
79-
for (const step of steps.items) {
80-
const stepUses = step.get("uses");
81-
const stepWith = step.get("with");
82-
83-
if (
84-
stepUses &&
85-
stepUses.includes("actions/setup-node") &&
86-
(!stepWith || !stepWith.get("cache"))
87-
) {
88-
if (!stepWith) {
89-
step.set("with", { cache });
90-
} else {
91-
stepWith.set("cache", cache);
92-
}
93-
94-
if (stepUses === "actions/setup-node@v1") {
95-
step.set("uses", "actions/setup-node@v2");
96-
}
97-
98-
cacheAdded = true;
99-
}
100-
}
101-
}
102-
103-
return cacheAdded
104-
? prettier.format(yamlDocument.toString(), {
105-
parser: "yaml",
106-
})
107-
: null;
108-
};
67+
filesToEdit[element.path] = getAddCacheToSetupNodeFunction(cache)
10968
});
11069

11170
const prCreated = await composeCreatePullRequest(octokit, {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Build
2+
on:
3+
push:
4+
branches-ignore:
5+
- main
6+
- staging
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
node-version: [14.x]
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Use Node.js ${{ matrix.node-version }}
16+
uses: actions/setup-node@v1
17+
with:
18+
node-version: ${{ matrix.node-version }}
19+
cache: npm
20+
- run: npm ci
21+
- run: npm run build --if-present
22+
env:
23+
CI: true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Build
2+
on:
3+
push:
4+
branches-ignore:
5+
- main
6+
- staging
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
node-version: [14.x]
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Use Node.js ${{ matrix.node-version }}
16+
uses: actions/setup-node@v1
17+
with:
18+
node-version: ${{ matrix.node-version }}
19+
cache: npm
20+
- run: npm ci
21+
- run: npm run build --if-present
22+
env:
23+
CI: true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Build
2+
on:
3+
push:
4+
branches-ignore:
5+
- main
6+
- staging
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
node-version: [14.x]
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Use Node.js ${{ matrix.node-version }}
16+
uses: actions/setup-node@v1
17+
with:
18+
node-version: ${{ matrix.node-version }}
19+
cache: npm
20+
- run: npm ci
21+
- run: npm run build --if-present
22+
env:
23+
CI: true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Build
2+
on:
3+
push:
4+
branches-ignore:
5+
- main
6+
- staging
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
node-version: [14.x]
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Use Node.js ${{ matrix.node-version }}
16+
uses: actions/setup-node@v1
17+
with:
18+
node-version: ${{ matrix.node-version }}
19+
cache: npm
20+
- run: npm ci
21+
- run: npm run build --if-present
22+
env:
23+
CI: true
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Test
2+
on:
3+
push:
4+
branches:
5+
- "main"
6+
pull_request:
7+
jobs:
8+
main:
9+
runs-on: ${{ matrix.os }}
10+
strategy:
11+
matrix:
12+
os: [ubuntu-latest, windows-latest, macOS-latest]
13+
node-version: [10.x, 12.x, 14.x, 15.x]
14+
steps:
15+
- uses: actions/checkout@v2
16+
- uses: actions/setup-node@v1
17+
with:
18+
node-version: ${{ matrix.node-version }}
19+
20+
- name: Cache node_modules
21+
id: cache-node_modules
22+
uses: actions/cache@v2
23+
with:
24+
path: node_modules
25+
key: node_modules-${{ matrix.os }}-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }}
26+
27+
- name: npm ci
28+
if: steps.cache-node_modules.outputs.cache-hit != 'true'
29+
run: npm ci
30+
- name: Jest
31+
run: npx --no-install jest
32+
- name: CLI sanity
33+
run: npm run test:cli-sanity
34+
- name: CLI sanity warning
35+
run: npm run test:cli-sanity-warning
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Test
2+
on:
3+
push:
4+
branches:
5+
- "main"
6+
pull_request: null
7+
jobs:
8+
main:
9+
runs-on: ${{ matrix.os }}
10+
strategy:
11+
matrix:
12+
os: [ubuntu-latest, windows-latest, macOS-latest]
13+
node-version: [10.x, 12.x, 14.x, 15.x]
14+
steps:
15+
- uses: actions/checkout@v2
16+
- uses: actions/setup-node@v2
17+
with:
18+
node-version: ${{ matrix.node-version }}
19+
cache: npm
20+
21+
- name: Cache node_modules
22+
id: cache-node_modules
23+
uses: actions/cache@v2
24+
with:
25+
path: node_modules
26+
key: node_modules-${{ matrix.os }}-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }}
27+
28+
- name: npm ci
29+
if: steps.cache-node_modules.outputs.cache-hit != 'true'
30+
run: npm ci
31+
- name: Jest
32+
run: npx --no-install jest
33+
- name: CLI sanity
34+
run: npm run test:cli-sanity
35+
- name: CLI sanity warning
36+
run: npm run test:cli-sanity-warning
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Test
2+
on:
3+
push:
4+
branches:
5+
- "main"
6+
pull_request:
7+
jobs:
8+
main:
9+
runs-on: ${{ matrix.os }}
10+
strategy:
11+
matrix:
12+
os: [ubuntu-latest, windows-latest, macOS-latest]
13+
node-version: [10.x, 12.x, 14.x, 15.x]
14+
steps:
15+
- uses: actions/checkout@v2
16+
- uses: actions/setup-node@v1
17+
with:
18+
node-version: ${{ matrix.node-version }}
19+
20+
- name: Cache node_modules
21+
id: cache-node_modules
22+
uses: actions/cache@v2
23+
with:
24+
path: node_modules
25+
key: node_modules-${{ matrix.os }}-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }}
26+
27+
- name: npm ci
28+
if: steps.cache-node_modules.outputs.cache-hit != 'true'
29+
run: npm ci
30+
- name: Jest
31+
run: npx --no-install jest
32+
- name: CLI sanity
33+
run: npm run test:cli-sanity
34+
- name: CLI sanity warning
35+
run: npm run test:cli-sanity-warning

0 commit comments

Comments
 (0)