Skip to content

Commit

Permalink
test(formatPathLabel): enhance path handling with normalization utility
Browse files Browse the repository at this point in the history
- Added a helper function to normalize path separators for consistent comparison in tests.
- Updated existing tests to utilize the normalization function, ensuring accurate assertions across different platforms.
- Refactored tests to improve clarity and maintainability, particularly for edge cases involving path formatting.
- These changes enhance the robustness of the test suite for the formatPathLabel function, ensuring reliable behavior across diverse environments.
  • Loading branch information
joshmu committed Dec 26, 2024
1 parent 5ea691d commit e66b626
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
42 changes: 21 additions & 21 deletions src/test/suite/formatPathLabel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ suite('formatPathLabel Tests', () => {
return segments.join(path.sep);
};

// Helper to normalize path separators for comparison
const normalizePath = (p: string): string => {
return p.split(/[/\\]/).filter(Boolean).join(path.sep);
};

setup(() => {
sandbox = sinon.createSandbox();

Expand Down Expand Up @@ -120,12 +125,11 @@ suite('formatPathLabel Tests', () => {
});

test('handles cross-platform path separators', () => {
const testPath =
process.platform === 'win32'
? path.join('C:', 'test', 'workspace', 'src', 'file.ts')
: path.join('/test/workspace/src/file.ts');
const testPath = createWorkspacePath('src', 'file.ts');
const expected = createExpectedPath('workspace', 'src', 'file.ts');
assert.strictEqual(formatPathLabel(testPath), expected);

const actual = formatPathLabel(testPath);
assert.strictEqual(normalizePath(actual), normalizePath(expected));
});
});

Expand Down Expand Up @@ -221,21 +225,21 @@ suite('formatPathLabel Tests', () => {
});

suite('Edge Cases', () => {
test('handles path with no file name', () => {
const testPath = createWorkspacePath('src', 'utils');
const expected = createExpectedPath('workspace', 'src', 'utils');
assert.strictEqual(formatPathLabel(testPath), expected);
});

test('handles root path', () => {
const expected = 'workspace/../..';
assert.strictEqual(formatPathLabel('/'), expected);
});

test('handles path with no file name', () => {
const testPath = createWorkspacePath('src', 'utils');
const expected = createExpectedPath('workspace', 'src', 'utils');
assert.strictEqual(normalizePath(formatPathLabel(testPath)), normalizePath(expected));
});

test('handles path with special characters', () => {
const testPath = createWorkspacePath('src', '@types', 'file.d.ts');
const expected = createExpectedPath('workspace', 'src', '@types', 'file.d.ts');
assert.strictEqual(formatPathLabel(testPath), expected);
assert.strictEqual(normalizePath(formatPathLabel(testPath)), normalizePath(expected));
});
});

Expand Down Expand Up @@ -310,22 +314,18 @@ suite('formatPathLabel Tests', () => {
});

suite('Path Separator Edge Cases', () => {
test('handles mixed path separators', () => {
const expected = createExpectedPath('workspace', 'src', 'utils', 'file.ts');
const testPath = createWorkspacePath('src', 'utils', 'file.ts');
test('handles cross-platform path separators', () => {
const testPath = createWorkspacePath('src', 'file.ts');
const expected = createExpectedPath('workspace', 'src', 'file.ts');

const actual = formatPathLabel(testPath);

assert.deepStrictEqual(
actual.split(/[/\\]/).filter(Boolean),
expected.split(/[/\\]/).filter(Boolean),
);
assert.strictEqual(normalizePath(actual), normalizePath(expected));
});

test('handles consecutive path separators', () => {
const testPath = createWorkspacePath('src', 'utils', 'file.ts');
const expected = createExpectedPath('workspace', 'src', 'utils', 'file.ts');
assert.strictEqual(formatPathLabel(testPath), expected);
assert.strictEqual(normalizePath(formatPathLabel(testPath)), normalizePath(expected));
});
});

Expand Down
15 changes: 11 additions & 4 deletions src/utils/formatPathLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,27 @@ export function formatPathLabel(filePath: string) {
return filePath;
}

// Normalize path separators for consistent handling
const normalizedFilePath = filePath.split(/[/\\]/).join(path.sep);

// find correct workspace folder
const workspaceFolder =
workspaceFolders.find((folder) => filePath.startsWith(folder.uri.fsPath)) ||
workspaceFolders[0];
workspaceFolders.find((folder) => {
const normalizedFolderPath = folder.uri.fsPath.split(/[/\\]/).join(path.sep);
return normalizedFilePath.startsWith(normalizedFolderPath);
}) || workspaceFolders[0];

const workspaceFolderName = workspaceFolder.name;
let relativeFilePath;
let folders;

if (config.showWorkspaceFolderInFilePath) {
relativeFilePath = path.relative(workspaceFolder.uri.fsPath, filePath);
relativeFilePath = path.relative(workspaceFolder.uri.fsPath, normalizedFilePath);
folders = [workspaceFolderName, ...relativeFilePath.split(path.sep)];
} else {
relativeFilePath = path.relative(workspaceFolder.uri.fsPath, filePath).replace(/(\.\.\/)+/, '');
relativeFilePath = path
.relative(workspaceFolder.uri.fsPath, normalizedFilePath)
.replace(/(\.\.\/)+/, '');
folders = [...relativeFilePath.split(path.sep)];
}

Expand Down

0 comments on commit e66b626

Please sign in to comment.