Skip to content

Commit 5cc73dc

Browse files
authoredAug 31, 2022
fix(script): apply proper indentation when modifying/creating a JSON (#30)
fix(script): apply proper indentation when modifying/creating a JSON file
1 parent 0e763e9 commit 5cc73dc

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed
 

‎script.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export async function script(octokit, repository, options) {
6767

6868
renovateConfigObj.extends = newExtends;
6969

70-
return JSON.stringify(jsonFile);
70+
return JSON.stringify(jsonFile, null, "\t");
7171
},
7272
message: "build: renovate setup",
7373
});

‎script.test.js

+55
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,61 @@ test("adds 'renovate' entry to package.json if it did not exist", async () => {
6868
});
6969
});
7070

71+
test("preserves spacing for JSON files", async () => {
72+
const path = "renovate.json";
73+
74+
const originalPackageJson = {
75+
name: "octoherd-cli",
76+
version: "0.0.0",
77+
description: "",
78+
main: "index.js",
79+
scripts: {
80+
test: 'echo "Error: no test specified" && exit 1',
81+
},
82+
author: "",
83+
license: "ISC",
84+
};
85+
86+
nock("https://api.github.com")
87+
.get(`/repos/${repository.owner.login}/${repository.name}/contents/${path}`)
88+
.reply(200, {
89+
type: "file",
90+
sha: "randomSha",
91+
content: Buffer.from(JSON.stringify(originalPackageJson)).toString(
92+
"base64"
93+
),
94+
})
95+
.put(
96+
`/repos/${repository.owner.login}/${repository.name}/contents/${path}`,
97+
(body) => {
98+
equal(
99+
Buffer.from(body.content, "base64").toString(),
100+
"{\n" +
101+
'\t"name": "octoherd-cli",\n' +
102+
'\t"version": "0.0.0",\n' +
103+
'\t"description": "",\n' +
104+
'\t"main": "index.js",\n' +
105+
'\t"scripts": {\n' +
106+
'\t\t"test": "echo \\"Error: no test specified\\" && exit 1"\n' +
107+
"\t},\n" +
108+
'\t"author": "",\n' +
109+
'\t"license": "ISC",\n' +
110+
'\t"extends": [\n' +
111+
'\t\t"github>octoherd/.github"\n' +
112+
"\t]\n" +
113+
"}"
114+
);
115+
116+
return true;
117+
}
118+
)
119+
.reply(200, { commit: { html_url: "link to commit" } });
120+
121+
await script(getOctokitForTests(), repository, {
122+
extends: "github>octoherd/.github",
123+
path,
124+
});
125+
});
71126
test("adds 'renovate' entry ONLY to package.json files", async () => {
72127
const path = "renovate.json";
73128

0 commit comments

Comments
 (0)