Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(relative-tag): missing ** in pattern & generate invalid ignore pa…
- Loading branch information
Showing
3 changed files
with
92 additions
and
20 deletions.
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
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,52 @@ | ||
'use strict' | ||
|
||
const formatRelativeIgnoreFile = require('../formatRelativeIgnoreFile') | ||
|
||
describe('formatRelativeIgnoreFile', () => { | ||
const relativeDir = 'output' | ||
|
||
it('should ignore empty lines', () => { | ||
const input = '\n'.repeat(10) | ||
expect(formatRelativeIgnoreFile(input, relativeDir)).toBe(input) | ||
}) | ||
|
||
it('should ignore empty spaces', () => { | ||
const input = ' \n'.repeat(10) | ||
expect(formatRelativeIgnoreFile(input, relativeDir)).toBe(input) | ||
}) | ||
|
||
it('should ignore comments', () => { | ||
const input = '# comments\n'.repeat(10) | ||
expect(formatRelativeIgnoreFile(input, relativeDir)).toBe(input) | ||
}) | ||
|
||
it('should prepend relative path with **', () => { | ||
expect(formatRelativeIgnoreFile('a', relativeDir)).toBe('output/**/a') | ||
expect(formatRelativeIgnoreFile('a/', relativeDir)).toBe('output/**/a/') | ||
|
||
expect(formatRelativeIgnoreFile('!a', relativeDir)).toBe('!output/**/a') | ||
expect(formatRelativeIgnoreFile('!a/', relativeDir)).toBe('!output/**/a/') | ||
}) | ||
|
||
it('should not prepend relative path with ** if already starts with **', () => { | ||
expect(formatRelativeIgnoreFile('**/a', relativeDir)).toBe('output/**/a') | ||
expect(formatRelativeIgnoreFile('/**/a', relativeDir)).toBe('output/**/a') | ||
|
||
expect(formatRelativeIgnoreFile('!**/a', relativeDir)).toBe('!output/**/a') | ||
expect(formatRelativeIgnoreFile('!/**/a', relativeDir)).toBe('!output/**/a') | ||
}) | ||
|
||
it('should prepend relative path without **', () => { | ||
expect(formatRelativeIgnoreFile('/a', relativeDir)).toBe('output/a') | ||
expect(formatRelativeIgnoreFile('/a/', relativeDir)).toBe('output/a/') | ||
expect(formatRelativeIgnoreFile('a/b', relativeDir)).toBe('output/a/b') | ||
expect(formatRelativeIgnoreFile('a/**', relativeDir)).toBe('output/a/**') | ||
expect(formatRelativeIgnoreFile('a//', relativeDir)).toBe('output/a//') | ||
|
||
expect(formatRelativeIgnoreFile('!/a', relativeDir)).toBe('!output/a') | ||
expect(formatRelativeIgnoreFile('!/a/', relativeDir)).toBe('!output/a/') | ||
expect(formatRelativeIgnoreFile('!a/b', relativeDir)).toBe('!output/a/b') | ||
expect(formatRelativeIgnoreFile('!a/**', relativeDir)).toBe('!output/a/**') | ||
expect(formatRelativeIgnoreFile('!a//', relativeDir)).toBe('!output/a//') | ||
}) | ||
}) |
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,37 @@ | ||
'use strict' | ||
|
||
const { LINE_BREAK } = require('../constants') | ||
|
||
const prependRelativePath = (pattern, relativeDir) => { | ||
if (pattern.startsWith('!')) { | ||
return '!' + prependRelativePath(pattern.substring(1), relativeDir) | ||
} | ||
|
||
if (pattern.startsWith('/')) { | ||
return [relativeDir, pattern.substring(1)].join('/') | ||
} | ||
|
||
if (pattern.includes('/') && pattern.indexOf('/') !== pattern.length - 1) { | ||
return [relativeDir, pattern].join('/') | ||
} | ||
|
||
return [relativeDir, '**', pattern].join('/') | ||
} | ||
|
||
module.exports = function formatRelativeIgnoreFile(fileContent, relativeDir) { | ||
const splittedFileContent = fileContent.split(LINE_BREAK) | ||
|
||
const edittedFileContent = splittedFileContent.map((line) => { | ||
if (!line.trim()) { | ||
return line | ||
} | ||
|
||
if (line.startsWith('#')) { | ||
return line | ||
} | ||
|
||
return prependRelativePath(line, relativeDir) | ||
}) | ||
|
||
return edittedFileContent.join(LINE_BREAK) | ||
} |