Skip to content

Commit

Permalink
chore: fix test, eslint and prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Apr 28, 2024
1 parent a2fca02 commit 59d4e3b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 39 deletions.
21 changes: 8 additions & 13 deletions src/__tests__/createGeneralTOC.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync, statSync } from 'fs';
import { statSync } from 'fs';
import { join } from 'path';

import readdir from 'recursive-readdir';
Expand All @@ -11,17 +11,12 @@ const dataDir = join(__dirname, 'general');
test('createGeneralTOC', async () => {
await createGeneralTOC(__dirname, { dataDir });

const files = (await readdir(dataDir)).sort()
.filter((file) => file.match(/\.json$/))
const files = (await readdir(dataDir))
.sort()
.filter((file) => file.match(/\.json$/));

const sizes = files
.map((file) => {
return statSync(file).size;
});
expect(sizes).toStrictEqual([1656,
832,
1127,
96,
487,
250,]);
const sizes = files.map((file) => {
return statSync(file).size;
});
expect(sizes).toStrictEqual([1656, 832, 1127, 96, 487, 250]);
});
9 changes: 6 additions & 3 deletions src/__tests__/createMolfilesFromFiles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ test('more than one .txt file found', async () => {
await createMolfilesFromFiles(__dirname, {
dataDir: join(__dirname, 'smiles/many'),
});
expect(logs).toStrictEqual(['More than one file with .txt extension found']);
expect(logs[0]).toContain(['more than one file with .txt extension found']);
expect(logs[1]).toContain(['no file with .sdf extension found']);
console.log = originalConsoleLog;
});

Expand All @@ -25,7 +26,8 @@ test('no .txt file', async () => {
await createMolfilesFromFiles(__dirname, {
dataDir: join(__dirname, 'smiles/none'),
});
expect(logs).toStrictEqual(['No file with .txt extension found']);
expect(logs[0]).toContain(['no file with .txt extension found']);
expect(logs[1]).toContain(['no file with .sdf extension found']);
console.log = originalConsoleLog;
});

Expand All @@ -37,6 +39,7 @@ test('one .txt file', async () => {
await createMolfilesFromFiles(__dirname, {
dataDir: join(__dirname, 'smiles/one'),
});
expect(logs).toHaveLength(3);
expect(logs).toHaveLength(1);
expect(logs[0]).toContain(['already contains folder 01']);
console.log = originalConsoleLog;
});
6 changes: 1 addition & 5 deletions src/commands/createExercisesCorrection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import { join } from 'path';
import debugFct from 'debug';
import OCL from 'openchemlib';



const debug = debugFct('nmrium.createExercisesCorrection');



const { Molecule } = OCL;
/**
* Add the links based on all the available toc files
Expand All @@ -19,7 +15,7 @@ const { Molecule } = OCL;
*/
export async function createExercisesCorrection(commandDir, options = {}) {
const { dataDir = commandDir } = options;
debug(`Creating correction of: ${dataDir}`)
debug(`Creating correction of: ${dataDir}`);

const correctionDir = join(dataDir, 'correction');
mkdirSync(correctionDir, { recursive: true });
Expand Down
14 changes: 8 additions & 6 deletions src/commands/createMolfilesFromFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ const { Molecule } = OCL;
export async function createMolfilesFromFiles(commandDir, options = {}) {
const { dataDir = commandDir } = options;

const folders = readdirSync(dataDir, { withFileTypes: true, recursive: true })
.filter((entity) => entity.isDirectory())
.map((entity) => join(entity.path, entity.name))
.filter((name) => !name.match(/\/\d\d$/))
.filter((name) => !name.match(/\/\./));
const folders = [
dataDir,
...readdirSync(dataDir, { withFileTypes: true, recursive: true })
.filter((entity) => entity.isDirectory())
.map((entity) => join(entity.path, entity.name))
.filter((name) => !name.match(/\/\d\d$/))
.filter((name) => !name.match(/\/\./)),
];

for (let folder of folders) {
if (existsSync(join(folder, '01'))) {
Expand Down Expand Up @@ -80,7 +83,6 @@ function getFileWithExtension(folder, extension) {

function fromSmilesFile(folder) {
// we search for the file that contains the smiles. It should end with .txt

const file = getFileWithExtension(folder, '.txt');
if (!file) return;

Expand Down
10 changes: 4 additions & 6 deletions src/commands/toc/getFolderConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ export function getFolderConfig(folder) {
loadSettings(folderConfig, folder);
loadSettings(defaultFolderConfig, folder);


return {
folderConfig: { ...defaultFolderConfig, ...folderConfig }, defaultFolderConfig
folderConfig: { ...defaultFolderConfig, ...folderConfig },
defaultFolderConfig,
};
}

function loadSettings(config, folder) {
if (config.settingsFilename) {
const settingsFilename = join(folder, config.settingsFilename);
if (existsSync(settingsFilename)) {
config.settings = JSON.parse(
readFileSync(settingsFilename, 'utf8'),
);
config.settings = JSON.parse(readFileSync(settingsFilename, 'utf8'));
}
}
}
}
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable no-unused-expressions */
import { join } from 'path';

import { hideBin } from 'yargs/helpers';
import yargs from 'yargs/yargs';

Expand Down Expand Up @@ -107,13 +109,11 @@ yargs(hideBin(normalizeArgv(process.argv)))

.demandCommand().argv;

function normalizeArgv(argv) {

function normalizeArgv(argv = {}) {
if (argv.dataDir) {
if (!dataDir.startsWith('/')) {
if (!argv.dataDir.startsWith('/')) {
argv.dataDir = join(homeDir, argv.dataDir);
}
}
return argv

}
return argv;
}

0 comments on commit 59d4e3b

Please sign in to comment.