Skip to content

Commit

Permalink
chore: fix tests on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
gmickel committed Jul 20, 2024
1 parent 21743e2 commit 7527a51
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
12 changes: 12 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ if (__dirname.includes(`${path.sep}dist`)) {
cliPath = path.resolve(__dirname, 'src', 'cli', 'index.ts');
}

// Add this function to determine the templates directory
function getTemplatesDir() {
const isProduction = __dirname.includes(`${path.sep}dist`);
if (isProduction) {
return path.resolve(__dirname, '..');
}
return path.resolve(__dirname, 'src', 'templates');
}

// Set the TEMPLATES_DIR environment variable
process.env.TEMPLATES_DIR = getTemplatesDir();

import(url.pathToFileURL(cliPath).href)
.then((cliModule) => {
if (cliModule.default) {
Expand Down
24 changes: 14 additions & 10 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ import { processFiles } from '../core/file-processor';
import { generateMarkdown } from '../core/markdown-generator';
import { interactiveMode } from './interactive-filtering';

const isProduction = path
.dirname(new URL(import.meta.url).pathname)
.includes('/dist/');
const templatesDir = isProduction
? path.resolve(path.dirname(new URL(import.meta.url).pathname), '../')
: path.resolve(
path.dirname(new URL(import.meta.url).pathname),
'../templates',
);

const templatesDir =
process.env.TEMPLATES_DIR ??
(() => {
const isProduction = path
.dirname(new URL(import.meta.url).pathname)
.includes(`${path.sep}dist`);
return isProduction
? path.resolve(path.dirname(new URL(import.meta.url).pathname), '..')
: path.resolve(
path.dirname(new URL(import.meta.url).pathname),
'..',
'templates',
);
})();
const program = new Command();

export function cli(args: string[]) {
Expand Down
8 changes: 6 additions & 2 deletions tests/integration/markdown-generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ function decodeHTMLEntities(text: string): string {

describe('Markdown Generation Integration', () => {
const fixturesPath = path.resolve(__dirname, '../fixtures');
const customTemplatePath = path.join(fixturesPath, 'custom-template.hbs');
const defaultTemplatePath = path.join(fixturesPath, 'default.hbs');
const normalizedFixturesPath = path.normalize(fixturesPath);
const customTemplatePath = path.join(
normalizedFixturesPath,
'custom-template.hbs',
);
const defaultTemplatePath = path.join(normalizedFixturesPath, 'default.hbs');

const mockFiles: FileInfo[] = [
{
Expand Down
33 changes: 17 additions & 16 deletions tests/unit/file-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ vi.mock('../../src/utils/file-cache');

describe('processFiles', () => {
const fixturesPath = path.resolve(__dirname, '../fixtures/test-project');
const tempGitignorePath = path.join(fixturesPath, '.gitignore');
const normalizedFixturesPath = path.normalize(fixturesPath);
const tempGitignorePath = path.join(normalizedFixturesPath, '.gitignore');

beforeEach(() => {
vi.clearAllMocks();
Expand All @@ -27,15 +28,15 @@ describe('processFiles', () => {
read() {
const files = ['src/main.js', 'src/utils.ts', 'package.json'];
for (const file of files) {
this.push(path.join(fixturesPath, file));
this.push(path.join(normalizedFixturesPath, file));
}
this.push(null);
},
}),
);

const mockFileInfo: FileInfo = {
path: path.join(fixturesPath, 'src/main.js'),
path: path.join(normalizedFixturesPath, 'src/main.js'),
extension: 'js',
language: 'javascript',
size: 100,
Expand All @@ -47,7 +48,7 @@ describe('processFiles', () => {
vi.mocked(Piscina.prototype.run).mockResolvedValue(mockFileInfo);

const result = await processFiles({
path: fixturesPath,
path: normalizedFixturesPath,
gitignorePath: tempGitignorePath,
});

Expand All @@ -64,7 +65,7 @@ describe('processFiles', () => {
read() {
const files = ['src/main.js', 'src/utils.ts', 'package.json'];
for (const file of files) {
this.push(path.join(fixturesPath, file));
this.push(path.join(normalizedFixturesPath, file));
}
this.push(null);
},
Expand All @@ -73,7 +74,7 @@ describe('processFiles', () => {

const mockFileInfos: { [key: string]: FileInfo } = {
'src/main.js': {
path: path.join(fixturesPath, 'src/main.js'),
path: path.join(normalizedFixturesPath, 'src/main.js'),
extension: 'js',
language: 'javascript',
size: 100,
Expand All @@ -82,7 +83,7 @@ describe('processFiles', () => {
content: 'mock content for main.js',
},
'src/utils.ts': {
path: path.join(fixturesPath, 'src/utils.ts'),
path: path.join(normalizedFixturesPath, 'src/utils.ts'),
extension: 'ts',
language: 'typescript',
size: 100,
Expand All @@ -91,7 +92,7 @@ describe('processFiles', () => {
content: 'mock content for utils.ts',
},
'package.json': {
path: path.join(fixturesPath, 'package.json'),
path: path.join(normalizedFixturesPath, 'package.json'),
extension: 'json',
language: 'json',
size: 100,
Expand All @@ -103,13 +104,13 @@ describe('processFiles', () => {

vi.mocked(Piscina.prototype.run).mockImplementation(
async ({ filePath }) => {
const relativePath = path.relative(fixturesPath, filePath);
const relativePath = path.relative(normalizedFixturesPath, filePath);
return mockFileInfos[relativePath];
},
);

const result = await processFiles({
path: fixturesPath,
path: normalizedFixturesPath,
gitignorePath: tempGitignorePath,
customIgnores: ['**/*.js'],
});
Expand All @@ -120,7 +121,7 @@ describe('processFiles', () => {

it('should use cache for unchanged files', async () => {
const cachedFile: FileInfo = {
path: path.join(fixturesPath, 'src/main.js'),
path: path.join(normalizedFixturesPath, 'src/main.js'),
extension: 'js',
language: 'javascript',
content: 'cached content',
Expand All @@ -140,7 +141,7 @@ describe('processFiles', () => {
read() {
const files = ['src/main.js', 'src/utils.ts', 'package.json'];
for (const file of files) {
this.push(path.join(fixturesPath, file));
this.push(path.join(normalizedFixturesPath, file));
}
this.push(null);
},
Expand All @@ -150,7 +151,7 @@ describe('processFiles', () => {
const mockFileInfos: { [key: string]: FileInfo } = {
'src/main.js': cachedFile,
'src/utils.ts': {
path: path.join(fixturesPath, 'src/utils.ts'),
path: path.join(normalizedFixturesPath, 'src/utils.ts'),
extension: 'ts',
language: 'typescript',
size: 100,
Expand All @@ -159,7 +160,7 @@ describe('processFiles', () => {
content: 'mock content',
},
'package.json': {
path: path.join(fixturesPath, 'package.json'),
path: path.join(normalizedFixturesPath, 'package.json'),
extension: 'json',
language: 'json',
size: 100,
Expand All @@ -171,13 +172,13 @@ describe('processFiles', () => {

vi.mocked(Piscina.prototype.run).mockImplementation(
async ({ filePath }) => {
const relativePath = path.relative(fixturesPath, filePath);
const relativePath = path.relative(normalizedFixturesPath, filePath);
return mockFileInfos[relativePath];
},
);

const result = await processFiles({
path: fixturesPath,
path: normalizedFixturesPath,
gitignorePath: tempGitignorePath,
});

Expand Down

0 comments on commit 7527a51

Please sign in to comment.