Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
25 changes: 12 additions & 13 deletions packages/cli/test/functional/testUtil/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ function _readFileSync(...paths: string[]) {
}

/**
* Filters out .page-vue-render.js files from the list of file paths.
* Filters out .page-vue-render.js and .DS_Store files from the list of file paths.
* @param {string[]} filePaths - List of file paths
* @returns {string[]} Filtered list without *.page-vue-render.js files
* @returns {string[]} Filtered list without generated render files and macOS metadata
*/
function filterPageVueRenderFiles(filePaths: string[]) {
return filePaths.filter(p => !p.endsWith('.page-vue-render.js'));
function filterIgnoredFiles(filePaths: string[]) {
return filePaths.filter(p => !p.endsWith('.page-vue-render.js') && !p.endsWith('.DS_Store'));
}

/**
Expand Down Expand Up @@ -87,8 +87,8 @@ function compare(root: string, expectedSiteRelativePath = 'expected', siteRelati
// Vue render JS files (*.page-vue-render.js) are not committed to version control,
// so we exclude them from the comparison to avoid false positive diffs.
// Note: Every non-includes .html file has a corresponding js render binary file
actualPaths = filterPageVueRenderFiles(actualPaths);
expectedPaths = filterPageVueRenderFiles(expectedPaths);
actualPaths = filterIgnoredFiles(actualPaths);
expectedPaths = filterIgnoredFiles(expectedPaths);

// Check for file existence of ignoredPaths and that they are present in actualPaths
if (ignoredPaths.length !== 0 && !_.isEqual(_.intersection(ignoredPaths, actualPaths), ignoredPaths)) {
Expand All @@ -100,20 +100,19 @@ function compare(root: string, expectedSiteRelativePath = 'expected', siteRelati
expectedPaths = expectedPaths.filter(p => !ignoredPaths.includes(p));

let error = false;
if (expectedPaths.length !== actualPaths.length) {
throw new Error('Unequal number of files! '
+ `Expected: ${expectedPaths.length}, Actual: ${actualPaths.length}`);
const missingPaths = _.difference(expectedPaths, actualPaths);
const unexpectedPaths = _.difference(actualPaths, expectedPaths);
if (missingPaths.length || unexpectedPaths.length) {
throw new Error('Different files built!'
+ `\nMissing files: ${JSON.stringify(missingPaths)}`
+ `\nUnexpected files: ${JSON.stringify(unexpectedPaths)}`);
}

/* eslint-disable no-continue */
for (let i = 0; i < expectedPaths.length; i += 1) {
const expectedFilePath = expectedPaths[i];
const actualFilePath = actualPaths[i];

if (expectedFilePath !== actualFilePath) {
throw new Error(`Different files built! Expected: ${expectedFilePath}, Actual: ${actualFilePath}`);
}

if (isBinary(expectedFilePath) || TEST_BLACKLIST.ignores(expectedFilePath)) {
continue;
}
Expand Down
24 changes: 24 additions & 0 deletions packages/cli/test/unit/compare.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import fs from 'fs';
import os from 'os';
import path from 'path';
import { compare } from '../functional/testUtil/compare.js';

test('compare ignores nested .DS_Store files and reports path differences', () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'markbind-compare-'));
const expectedDirectory = path.join(root, 'expected');
const actualDirectory = path.join(root, '_site');
try {
fs.mkdirSync(expectedDirectory);
fs.mkdirSync(path.join(actualDirectory, 'nested'), { recursive: true });
fs.writeFileSync(path.join(expectedDirectory, 'index.html'), 'same');
fs.writeFileSync(path.join(actualDirectory, 'index.html'), 'same');
fs.writeFileSync(path.join(actualDirectory, 'nested', '.DS_Store'), 'metadata');
expect(() => compare(root)).not.toThrow();
fs.writeFileSync(path.join(expectedDirectory, 'missing.html'), '');
fs.writeFileSync(path.join(actualDirectory, 'unexpected.html'), '');
expect(() => compare(root))
.toThrow('Missing files: ["missing.html"]\nUnexpected files: ["unexpected.html"]');
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});