Skip to content

Commit

Permalink
Update Windows tests to cover normalizedPath
Browse files Browse the repository at this point in the history
  • Loading branch information
dichovsky committed Aug 30, 2024
1 parent 2cc26aa commit 6b6c1f1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
35 changes: 24 additions & 11 deletions __tests__/normalize.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
import { expect, test } from 'vitest';
import { normalizePath } from '../src/normalize.path';
import { resolve } from 'path';

test('should normalize path ending with slash', () => {
const path = '/path/to/folder/';
const normalizedPath: string = normalizePath(path);
expect(normalizedPath).to.equal('/path/to/folder/');
if (process.platform === 'win32') {
expect(normalizedPath).to.equal('C:\\path\\to\\folder\\');

Check failure on line 8 in __tests__/normalize.test.ts

View workflow job for this annotation

GitHub Actions / windows (18)

__tests__/normalize.test.ts > should normalize path ending with slash

AssertionError: expected 'D:\path\to\folder\' to equal 'C:\path\to\folder\' Expected: "C:\path\to\folder\" Received: "D:\path\to\folder\" ❯ __tests__/normalize.test.ts:8:35

Check failure on line 8 in __tests__/normalize.test.ts

View workflow job for this annotation

GitHub Actions / windows (20)

__tests__/normalize.test.ts > should normalize path ending with slash

AssertionError: expected 'D:\path\to\folder\' to equal 'C:\path\to\folder\' Expected: "C:\path\to\folder\" Received: "D:\path\to\folder\" ❯ __tests__/normalize.test.ts:8:35
} else {
expect(normalizedPath).to.equal('/path/to/folder/');
}
});

test('should normalize path without ending slash', () => {
const path = '/path/to/folder';
const normalizedPath: string = normalizePath(path);
expect(normalizedPath).to.equal('/path/to/folder/');
if (process.platform === 'win32') {
expect(normalizedPath).to.equal('C:\\path\\to\\folder\\');

Check failure on line 18 in __tests__/normalize.test.ts

View workflow job for this annotation

GitHub Actions / windows (18)

__tests__/normalize.test.ts > should normalize path without ending slash

AssertionError: expected 'D:\path\to\folder\' to equal 'C:\path\to\folder\' Expected: "C:\path\to\folder\" Received: "D:\path\to\folder\" ❯ __tests__/normalize.test.ts:18:35

Check failure on line 18 in __tests__/normalize.test.ts

View workflow job for this annotation

GitHub Actions / windows (20)

__tests__/normalize.test.ts > should normalize path without ending slash

AssertionError: expected 'D:\path\to\folder\' to equal 'C:\path\to\folder\' Expected: "C:\path\to\folder\" Received: "D:\path\to\folder\" ❯ __tests__/normalize.test.ts:18:35
} else {
expect(normalizedPath).to.equal('/path/to/folder/');
}
});

test('should normalize empty path', () => {
const path = '';
const normalizedPath: string = normalizePath(path);
expect(normalizedPath).to.equal(`${resolve('./')}/`);
expect(() => normalizePath(path)).to.throw('Path cannot be empty');
});

test('should normalize root path', () => {
const path = '/';
const normalizedPath: string = normalizePath(path);
expect(normalizedPath).to.equal('/');

if (process.platform === 'win32') {
expect(normalizedPath).to.equal('C:\\');

Check failure on line 34 in __tests__/normalize.test.ts

View workflow job for this annotation

GitHub Actions / windows (18)

__tests__/normalize.test.ts > should normalize root path

AssertionError: expected 'D:\' to equal 'C:\' Expected: "C:\" Received: "D:\" ❯ __tests__/normalize.test.ts:34:35

Check failure on line 34 in __tests__/normalize.test.ts

View workflow job for this annotation

GitHub Actions / windows (20)

__tests__/normalize.test.ts > should normalize root path

AssertionError: expected 'D:\' to equal 'C:\' Expected: "C:\" Received: "D:\" ❯ __tests__/normalize.test.ts:34:35
} else {
expect(normalizedPath).to.equal('/');
}
});

test('should append trailing backslash if path ends with backslash on Windows systems', () => {
const path = 'C:\\Users\\test\\Projects\\pdf-to-png-converter\\src\\';
const normalizedPath = normalizePath(path);
expect(normalizedPath).to.equal('C:\\Users\\test\\Projects\\pdf-to-png-converter\\src\\');
});
if (process.platform === 'win32') {
test('should append trailing backslash if path ends with backslash on Windows systems', () => {
const path = 'C:\\Windows\\';
const normalizedPath = normalizePath(path);
expect(normalizedPath).to.equal('C:\\Windows\\');
});
}
2 changes: 0 additions & 2 deletions dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ RUN apt-get update && apt-get install -y build-essential libcairo2-dev libpango1
WORKDIR /usr/pkg/
COPY . .
RUN npm ci
RUN npm run build

CMD npm run docker:test
8 changes: 8 additions & 0 deletions src/compare.png.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { parse } from 'node:path';
import comparePng, { ComparePngOptions } from 'png-visual-compare';

/**
* Compares a PNG image with an expected PNG image.
* If the expected image does not exist, it creates it by copying the actual image.
* @param actualFilePathOrBuffer - The path or buffer of the actual PNG image.
* @param expectedFilePath - The path of the expected PNG image.
* @param opts - Optional compare options.
* @returns A promise that resolves to the comparison result.
*/
export function comparePNG(
actualFilePathOrBuffer: string | Buffer,
expectedFilePath: string,
Expand Down
16 changes: 13 additions & 3 deletions src/normalize.path.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { resolve, join, normalize } from "path/posix";
import { normalize, resolve } from 'node:path';

/**
* Normalizes a given path by ensuring it ends with the appropriate path separator.
*
* @param path - The path to be normalized.
* @returns The normalized path.
* @throws Error if the path is empty.
*/
export function normalizePath(path: string): string {
const resolvedPath: string = normalize(resolve(join(path)));
console.log(process.platform)
if (path === '') {
throw new Error('Path cannot be empty');
}
const resolvedPath: string = (normalize(resolve( path)));

if (process.platform === 'win32') {
if (resolvedPath.endsWith('\\')) {
return resolvedPath;
Expand Down

0 comments on commit 6b6c1f1

Please sign in to comment.