Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use correct highlight offset even when rootDir is not root package directory #34

Merged
merged 5 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/file_name_plugin/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default class FileNamePatternPrompt extends PatternPrompt {
path,
width,
);
return highlight(path, filePath, pattern, context.config.rootDir);
return highlight(path, filePath, pattern);
})
.map((item, i) => formatTypeaheadSelection(item, i, index, prompt))
.forEach(item => printTypeaheadItem(item, pipe));
Expand Down
4 changes: 4 additions & 0 deletions src/lib/__tests__/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ exports[`formatTestNameByPattern formats when testname="the test name", pattern=

exports[`formatTestNameByPattern formats when testname="the test name", pattern="the", and width="30" 1`] = `"</>the</><dim> test name</>"`;

exports[`highlight highlights match correctly when filePath="...s/utils/src/__tests__/hello-world.js" 1`] = `"<dim>...s/utils/src/__tests__/</></>hello</><dim>-world.js</>"`;

exports[`highlight highlights match correctly when filePath="libs/utils/src/__tests__/hello-world.js" 1`] = `"<dim>libs/utils/src/__tests__/</></>hello</><dim>-world.js</>"`;

exports[`trimAndFormatPath formats when testpath="/project/src/exactly/sep_and_basename.js", pad="6", and columns="29" 1`] = `"<dim>.../</><bold>sep_and_basename.js</>"`;

exports[`trimAndFormatPath formats when testpath="/project/src/gonna/fit/all.js", pad="6", and columns="80" 1`] = `"<dim>src/gonna/fit/</><bold>all.js</>"`;
Expand Down
20 changes: 19 additions & 1 deletion src/lib/__tests__/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { trimAndFormatPath, formatTestNameByPattern } from '../utils';
import {
trimAndFormatPath,
formatTestNameByPattern,
highlight,
} from '../utils';

jest.mock('chalk', () => {
const chalk = jest.requireActual('chalk');
Expand Down Expand Up @@ -52,3 +56,17 @@ describe('formatTestNameByPattern', () => {
},
);
});

describe('highlight', () => {
const rawPath =
'/Users/janedoe/monorepo/libs/utils/src/__tests__/hello-world.js';
const pattern = 'hello';

test.each`
filePath
${'libs/utils/src/__tests__/hello-world.js'}
${'...s/utils/src/__tests__/hello-world.js'}
`(`highlights match correctly when filePath="$filePath"`, ({ filePath }) => {
expect(highlight(rawPath, filePath, pattern)).toMatchSnapshot();
});
});
8 changes: 2 additions & 6 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export const highlight = (
rawPath: string,
filePath: string,
pattern: string,
rootDir: string,
) => {
const relativePathHead = './';

Expand All @@ -86,17 +85,14 @@ export const highlight = (
return chalk.dim(filePath);
}

let offset;
let trimLength;
const offset = rawPath.length - filePath.length;

let trimLength;
if (filePath.startsWith(TRIMMING_DOTS)) {
offset = rawPath.length - filePath.length;
trimLength = TRIMMING_DOTS.length;
} else if (filePath.startsWith(relativePathHead)) {
offset = rawPath.length - filePath.length;
trimLength = relativePathHead.length;
} else {
offset = rootDir.length + path.sep.length;
trimLength = 0;
}

Expand Down