This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
fix: project's package.json indentation is not persisted #727
Merged
rosen-vladimirov
merged 3 commits into
master
from
vladimirov/persist-package-json-indent
Dec 10, 2018
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,5 @@ jasmine-config/reporter.js.map | |
|
||
hooks | ||
.DS_Store | ||
|
||
!projectHelpers.spec.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
const { getIndentationCharacter, writePackageJson } = require("./projectHelpers"); | ||
const fs = require("fs"); | ||
|
||
describe('projectHelpers', () => { | ||
const originalReadFileSync = fs.readFileSync; | ||
const originalWriteFileSync = fs.writeFileSync; | ||
const tab = "\t"; | ||
const multipleSpaces = " "; | ||
const twoSpaces = " "; | ||
|
||
afterEach(() => { | ||
fs.readFileSync = originalReadFileSync; | ||
fs.writeFileSync = originalWriteFileSync; | ||
}); | ||
|
||
describe('getIndentationCharacter', () => { | ||
[ | ||
{ | ||
testName: 'returns two spaces when file starts with two spaces', | ||
input: `{${twoSpaces}"abc": "1"${twoSpaces}}`, | ||
expectedResult: twoSpaces | ||
}, | ||
{ | ||
testName: 'returns two spaces when file starts with two spaces and binary content is passed', | ||
input: Buffer.from(`{${twoSpaces}"abc": "1"${twoSpaces}}`), | ||
expectedResult: twoSpaces | ||
}, | ||
{ | ||
testName: 'returns empty string when file starts without any indentation', | ||
input: `{"abc": "1"}`, | ||
expectedResult: '' | ||
}, | ||
{ | ||
testName: 'returns tab when file starts with tab', | ||
input: `{${tab}"abc": "1"${tab}}`, | ||
expectedResult: tab | ||
}, | ||
{ | ||
testName: 'returns two spaces when file starts with two spaces and new line before them', | ||
input: `{\n${twoSpaces}"abc": "1"\n}`, | ||
expectedResult: twoSpaces | ||
}, | ||
{ | ||
testName: 'returns tab when file starts with tab and new line before them', | ||
input: `{\n${tab}"abc": "1"\n}`, | ||
expectedResult: tab | ||
}, | ||
{ | ||
testName: 'returns multiple spaces when file starts with multiple spaces and new line before them', | ||
input: `{\n${multipleSpaces}"abc": "1"\n}`, | ||
expectedResult: multipleSpaces | ||
} | ||
].forEach(({ testName, input, expectedResult }) => { | ||
it(testName, () => { | ||
expect(getIndentationCharacter(input)).toEqual(expectedResult); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('writePackageJson', () => { | ||
const mockFileSystemApi = () => { | ||
const data = { | ||
isWriteFileSyncCalled: false | ||
}; | ||
|
||
fs.readFileSync = (p) => { | ||
return JSON.stringify({ a: 1 }); | ||
}; | ||
|
||
fs.writeFileSync = (p, c) => { | ||
data.isWriteFileSyncCalled = true; | ||
}; | ||
|
||
return data; | ||
}; | ||
|
||
it('does not write package.json when content has not changed', () => { | ||
const data = mockFileSystemApi(); | ||
writePackageJson({ a: 1 }, "projDir"); | ||
expect(data.isWriteFileSyncCalled).toBe(false); | ||
}); | ||
|
||
it('writes content, when the new one is different from the current one', () => { | ||
const data = mockFileSystemApi(); | ||
writePackageJson({ b: 2 }, "projDir"); | ||
expect(data.isWriteFileSyncCalled).toBe(true); | ||
}); | ||
|
||
it('keeps indentation of the package.json when rewriting it', () => { | ||
let currentIndentSymbol = tab; | ||
fs.readFileSync = (p) => { | ||
return JSON.stringify({ a: 1 }, null, currentIndentSymbol); | ||
}; | ||
|
||
let writtenContent = null; | ||
fs.writeFileSync = (p, c) => { | ||
writtenContent = c; | ||
}; | ||
|
||
// Ensure tab indentation is persisted | ||
writePackageJson({ b: 2 }, "projDir"); | ||
expect(writtenContent).toBe(`{\n${tab}"b": 2\n}`); | ||
|
||
// Ensure spaces indentation is persisted | ||
currentIndentSymbol = multipleSpaces; | ||
writePackageJson({ b: 2 }, "projDir"); | ||
expect(writtenContent).toBe(`{\n${multipleSpaces}"b": 2\n}`); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return (matches && matches[1]) || defaultIndentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact we do not need default indentation, I've forgotten to remove the variable. I've already added a unit test that verifies we work correctly when the matches[1] group is undefined.