Skip to content

Commit

Permalink
Lint banner imports in tests
Browse files Browse the repository at this point in the history
Enforce importing from the same folder to avoid copy-paste errors,
running tests on old banners.

Ticket: https://phabricator.wikimedia.org/T369276
  • Loading branch information
gbirke committed Oct 23, 2024
1 parent 131f60c commit 6db472f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"lint:js": "eslint --ext .js,.vue,.ts,.cjs --cache --cache-location .eslintcache --ignore-path .gitignore .",
"lint:ts": "vue-tsc --noEmit",
"lint:css": "stylelint '**/*.{scss,vue}'",
"lint:test-imports": "node test/find_mismatched_imports.mjs",
"ci": "npm-run-all lint test",
"check-content-version": "ts-node check-content-version.ts",
"coverage": "vitest run --coverage",
Expand Down
48 changes: 48 additions & 0 deletions test/find_mismatched_imports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import fg from 'fast-glob';
import path from 'path';
import * as readline from "node:readline";
import * as fs from "node:fs";

const mismatchedImports = [];

// Find all test files in the banners folder, create a stream to iterate over them
const stream = fg.stream( 'test/banners/*/**/*.ts' );

// Regular expression to match import statements. We only care about the path part
// and that it contains the pattern "banners/CHANNEL_NAME/CAMPAIGN_NAME"
const importRegex = /import .+ from .+(banners\/\w+\/\w+)/;

for await ( const entry of stream ) {
// skip thank_you banners, they are not organized by campaign
if ( entry.includes( 'test/banners/thank_you' ) ) {
continue;
}

// Extract expected path from entry, e.g. "banners/desktop/C24_WMDE_Desktop_01"
const pathParts = entry.split( path.sep );
// drop "test" prefix
pathParts.shift();
const expectedPath = pathParts.slice( 0, 3 ).join( path.sep );

// Check each line of the file for mismatched imports
const lineReader = readline.createInterface( {
input: fs.createReadStream( entry )
} );
let lineNumber = 1;
for await ( const line of lineReader ) {
const match = line.match( importRegex );
if( match && !match[1].includes( expectedPath ) ) {
mismatchedImports.push( { entry, line, lineNumber } );
}
lineNumber++;
}
}

if ( mismatchedImports.length === 0 ) {
// Don't print anything if there are no mismatches, be a good POSIX citizen
process.exit( 0 );
}

console.log( 'The following banner tests import from the wrong campaign folders:' );
console.log( mismatchedImports );
process.exit( 1 );

0 comments on commit 6db472f

Please sign in to comment.