Skip to content

Commit

Permalink
fix(script): apply proper indentation when modifying/creating a JSON (#…
Browse files Browse the repository at this point in the history
…30)

fix(script): apply proper indentation when modifying/creating a JSON file
  • Loading branch information
oscard0m committed Aug 31, 2022
1 parent 0e763e9 commit 5cc73dc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function script(octokit, repository, options) {

renovateConfigObj.extends = newExtends;

return JSON.stringify(jsonFile);
return JSON.stringify(jsonFile, null, "\t");
},
message: "build: renovate setup",
});
Expand Down
55 changes: 55 additions & 0 deletions script.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,61 @@ test("adds 'renovate' entry to package.json if it did not exist", async () => {
});
});

test("preserves spacing for JSON files", async () => {
const path = "renovate.json";

const originalPackageJson = {
name: "octoherd-cli",
version: "0.0.0",
description: "",
main: "index.js",
scripts: {
test: 'echo "Error: no test specified" && exit 1',
},
author: "",
license: "ISC",
};

nock("https://api.github.com")
.get(`/repos/${repository.owner.login}/${repository.name}/contents/${path}`)
.reply(200, {
type: "file",
sha: "randomSha",
content: Buffer.from(JSON.stringify(originalPackageJson)).toString(
"base64"
),
})
.put(
`/repos/${repository.owner.login}/${repository.name}/contents/${path}`,
(body) => {
equal(
Buffer.from(body.content, "base64").toString(),
"{\n" +
'\t"name": "octoherd-cli",\n' +
'\t"version": "0.0.0",\n' +
'\t"description": "",\n' +
'\t"main": "index.js",\n' +
'\t"scripts": {\n' +
'\t\t"test": "echo \\"Error: no test specified\\" && exit 1"\n' +
"\t},\n" +
'\t"author": "",\n' +
'\t"license": "ISC",\n' +
'\t"extends": [\n' +
'\t\t"github>octoherd/.github"\n' +
"\t]\n" +
"}"
);

return true;
}
)
.reply(200, { commit: { html_url: "link to commit" } });

await script(getOctokitForTests(), repository, {
extends: "github>octoherd/.github",
path,
});
});
test("adds 'renovate' entry ONLY to package.json files", async () => {
const path = "renovate.json";

Expand Down

0 comments on commit 5cc73dc

Please sign in to comment.