Skip to content

Commit

Permalink
Isolate fixture folder in loader.test
Browse files Browse the repository at this point in the history
  • Loading branch information
Haruki1707 committed May 26, 2024
1 parent 53bc570 commit 6d525fd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
27 changes: 27 additions & 0 deletions test/folderIsolationUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import fs from 'fs'
import path from 'path'

export function isolateFolder(folderToIsolate: string, testName: string) {
const isolatedFolder = folderToIsolate + '_isolated_' + testName;
copyDirSync(folderToIsolate, isolatedFolder);

return isolatedFolder;
}

export function removeIsolatedFolder(isolatedFolder: string) {
fs.rmSync(isolatedFolder, { recursive: true, force: true })
}

function copyDirSync(source: string, destination: string) {
const exists = fs.existsSync(source);
const stats = exists && fs.statSync(source);
const isDirectory = exists && stats.isDirectory();
if (isDirectory) {
fs.mkdirSync(destination);
fs.readdirSync(source).forEach(childItemName => {
copyDirSync(path.join(source, childItemName), path.join(destination, childItemName));
});
} else {
fs.copyFileSync(source, destination);
}
}
36 changes: 20 additions & 16 deletions test/loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import fs from 'fs';
import { generateFiles, parseAll, parse, hasPhpTranslations, reset, prepareExtendedParsedLangFiles } from '../src/loader';
import { isolateFolder, removeIsolatedFolder } from './folderIsolationUtil'

beforeEach(() => reset(__dirname + '/fixtures/lang/'));
const isolatedFixtures = isolateFolder(__dirname + '/fixtures', 'loader');
afterAll(() => removeIsolatedFolder(isolatedFixtures));

beforeEach(() => reset(isolatedFixtures + '/lang/'));

it('creates a file for each lang', () => {
const langPath = __dirname + '/fixtures/lang/';
const langPath = isolatedFixtures + '/lang/';
const files = generateFiles(langPath, parseAll(langPath));

expect(files.length).toBe(3);
Expand All @@ -23,7 +27,7 @@ it('creates a file for each lang', () => {
});

it('merges published package translations into each lang .json', () => {
const langPath = __dirname + '/fixtures/lang/';
const langPath = isolatedFixtures + '/lang/';
const files = generateFiles(langPath, parseAll(langPath));

expect(files.length).toBe(3);
Expand All @@ -47,7 +51,7 @@ it('merges published package translations into each lang .json', () => {
});

it('includes .php lang file in subdirectory in .json', () => {
const langPath = __dirname + '/fixtures/lang/';
const langPath = isolatedFixtures + '/lang/';
const files = generateFiles(langPath, parseAll(langPath));
const langEn = JSON.parse(fs.readFileSync(langPath + files[0].name).toString());

Expand All @@ -57,7 +61,7 @@ it('includes .php lang file in subdirectory in .json', () => {
});

it('includes .php lang file in nested subdirectory in .json', () => {
const langPath = __dirname + '/fixtures/lang/';
const langPath = isolatedFixtures + '/lang/';
const files = generateFiles(langPath, parseAll(langPath));
const langEn = JSON.parse(fs.readFileSync(langPath + files[0].name).toString())

Expand All @@ -66,9 +70,9 @@ it('includes .php lang file in nested subdirectory in .json', () => {
})

it('inclues additional lang paths to load from', () => {
const langPath = __dirname + '/fixtures/lang/';
const langPath = isolatedFixtures + '/lang/';
const additionalLangPaths = [
__dirname + '/fixtures/locales/'
isolatedFixtures + '/locales/'
];

const langPaths = prepareExtendedParsedLangFiles([
Expand All @@ -84,9 +88,9 @@ it('inclues additional lang paths to load from', () => {
});

it('overwrites translations from additional lang paths', () => {
const langPath = __dirname + '/fixtures/lang/';
const langPath = isolatedFixtures + '/lang/';
const additionalLangPaths = [
__dirname + '/fixtures/locales/'
isolatedFixtures + '/locales/'
];

const langPaths = prepareExtendedParsedLangFiles([
Expand All @@ -103,33 +107,33 @@ it('overwrites translations from additional lang paths', () => {
});

it('transforms .php lang to .json', () => {
const lang = parse(fs.readFileSync(__dirname + '/fixtures/lang/en/auth.php').toString());
const lang = parse(fs.readFileSync(isolatedFixtures + '/lang/en/auth.php').toString());

expect(lang['failed']).toBe('These credentials do not match our records.');
});

it('transform nested .php lang files to .json', () => {
const langPt = parse(fs.readFileSync(__dirname + '/fixtures/lang/pt/auth.php').toString());
const langPt = parse(fs.readFileSync(isolatedFixtures + '/lang/pt/auth.php').toString());
expect(langPt['foo.level1.level2']).toBe('barpt');

const langEn = parse(fs.readFileSync(__dirname + '/fixtures/lang/en/auth.php').toString());
const langEn = parse(fs.readFileSync(isolatedFixtures + '/lang/en/auth.php').toString());
expect(langEn['foo.level1.level2']).toBe('baren');
});

it('transforms simple index array to .json', () => {
const lang = parse(fs.readFileSync(__dirname + '/fixtures/lang/en/auth.php').toString());
const lang = parse(fs.readFileSync(isolatedFixtures + '/lang/en/auth.php').toString());
expect(lang['arr.0']).toBe('foo');
expect(lang['arr.1']).toBe('bar');
});

it('ignores empty `array` or `null` translations', () => {
const lang = parse(fs.readFileSync(__dirname + '/fixtures/lang/en/ignore.php').toString());
const lang = parse(fs.readFileSync(isolatedFixtures + '/lang/en/ignore.php').toString());

expect(lang['empty_array']).toBe(undefined);
expect(lang['null']).toBe(undefined);
});

it('checks if there is .php translations', () => {
expect(hasPhpTranslations(__dirname + '/fixtures/lang/')).toBe(true);
expect(hasPhpTranslations(__dirname + '/fixtures/wronglangfolder/')).toBe(false);
expect(hasPhpTranslations(isolatedFixtures + '/lang/')).toBe(true);
expect(hasPhpTranslations(isolatedFixtures + '/wronglangfolder/')).toBe(false);
});

0 comments on commit 6d525fd

Please sign in to comment.