1
0
Fork 0

fix: more ES imports, some corresponding import changes, reset mock FS after every test

This commit is contained in:
Sean Sube 2022-02-02 07:46:24 -06:00
parent 5221258d26
commit 052ca736d2
14 changed files with 54 additions and 35 deletions

View File

@ -1,5 +1,6 @@
import { createLogger } from 'bunyan'; import { createLogger } from 'bunyan';
import { showCompletionScript } from 'yargs'; import yargs from 'yargs';
const { showCompletionScript } = yargs;
import { loadConfig } from './config/index.js'; import { loadConfig } from './config/index.js';
import { CONFIG_ARGS_NAME, CONFIG_ARGS_PATH, MODE, parseArgs } from './config/args.js'; import { CONFIG_ARGS_NAME, CONFIG_ARGS_PATH, MODE, parseArgs } from './config/args.js';

View File

@ -1,4 +1,5 @@
import { Options, usage } from 'yargs'; import yargs, { Options } from 'yargs';
const { usage } = yargs;
import { RuleSelector, RuleSources } from '../rule/index.js'; import { RuleSelector, RuleSources } from '../rule/index.js';
import { VERSION_INFO } from '../version.js'; import { VERSION_INFO } from '../version.js';

View File

@ -1,6 +1,8 @@
import { hasItems } from '@apextoaster/js-utils'; import { hasItems } from '@apextoaster/js-utils';
import { applyDiff, diff } from 'deep-diff'; import deepDiff from 'deep-diff';
import { cloneDeep } from 'lodash'; const { applyDiff, diff } = deepDiff;
import lodash from 'lodash';
const { cloneDeep } = lodash;
import { Rule } from './index.js'; import { Rule } from './index.js';
import { Visitor } from '../visitor/index.js'; import { Visitor } from '../visitor/index.js';

View File

@ -1,12 +1,15 @@
import { doesExist, hasItems } from '@apextoaster/js-utils'; import { doesExist, hasItems } from '@apextoaster/js-utils';
import { ErrorObject, ValidateFunction } from 'ajv'; import { ErrorObject, ValidateFunction } from 'ajv';
import { cloneDeep, defaultTo } from 'lodash';
import { LogLevel } from 'noicejs'; import { LogLevel } from 'noicejs';
import { Rule, RuleData } from './index.js'; import { Rule, RuleData } from './index.js';
import { Visitor, VisitorError, VisitorResult } from '../visitor/index.js'; import { Visitor, VisitorError, VisitorResult } from '../visitor/index.js';
import { VisitorContext } from '../visitor/VisitorContext.js'; import { VisitorContext } from '../visitor/VisitorContext.js';
import lodash from 'lodash';
const { cloneDeep, defaultTo } = lodash;
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/strict-boolean-expressions */ /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/strict-boolean-expressions */
const DEFAULT_FILTER = () => true; const DEFAULT_FILTER = () => true;

View File

@ -1,7 +1,9 @@
import { doesExist, ensureArray } from '@apextoaster/js-utils'; import { doesExist, ensureArray } from '@apextoaster/js-utils';
import { ValidateFunction } from 'ajv'; import { ValidateFunction } from 'ajv';
import { intersection } from 'lodash'; import lodash from 'lodash';
import { Minimatch } from 'minimatch'; const { intersection } = lodash;
import minimatch from 'minimatch';
const { Minimatch } = minimatch;
import { LogLevel } from 'noicejs'; import { LogLevel } from 'noicejs';
// import ruleSchemaData from '../../rules/salty-dog.yml'; // import ruleSchemaData from '../../rules/salty-dog.yml';

View File

@ -28,6 +28,12 @@ export function setFs(fs: Filesystem) {
}; };
} }
export function resetFs() {
readdir = promises.readdir;
readFile = promises.readFile;
writeFile = promises.writeFile;
}
export async function listFiles(path: string): Promise<Array<string>> { export async function listFiles(path: string): Promise<Array<string>> {
const dirs: Array<string> = [path]; const dirs: Array<string> = [path];
const files: Array<string> = []; const files: Array<string> = [];

View File

@ -2,7 +2,7 @@ import { expect } from 'chai';
import { vol } from 'memfs'; import { vol } from 'memfs';
import { main, STATUS_ERROR, STATUS_SUCCESS } from '../src/app.js'; import { main, STATUS_ERROR, STATUS_SUCCESS } from '../src/app.js';
import { Filesystem, readSource, setFs } from '../src/source.js'; import { Filesystem, readSource, resetFs, setFs } from '../src/source.js';
const TEST_ARGS_PRE = ['node', 'test']; const TEST_ARGS_PRE = ['node', 'test'];
const TEST_ARGS_CONFIG = ['--config-path', 'docs', '--config-name', 'config.yml']; const TEST_ARGS_CONFIG = ['--config-path', 'docs', '--config-name', 'config.yml'];
@ -39,6 +39,10 @@ describe('main app', async () => {
vol.reset(); vol.reset();
}); });
afterEach(() => {
resetFs();
});
it('completion should succeed', async () => { it('completion should succeed', async () => {
const status = await main(['node', 'test', 'complete']); const status = await main(['node', 'test', 'complete']);
expect(status).to.equal(STATUS_SUCCESS); expect(status).to.equal(STATUS_SUCCESS);

View File

@ -2,10 +2,10 @@ import { NotFoundError } from '@apextoaster/js-utils';
import { expect } from 'chai'; import { expect } from 'chai';
import { join } from 'path'; import { join } from 'path';
import { loadConfig, readConfig } from '../../src/config'; import { loadConfig, readConfig } from '../../src/config/index.js';
describe('load config helper', async () => { describe('load config helper', async () => {
it('should load an existing config', () => it('should load an existing config', async () =>
expect(loadConfig('config-stderr.yml', join(__dirname, '..', 'docs'))).to.eventually.deep.include({ expect(loadConfig('config-stderr.yml', join(__dirname, '..', 'docs'))).to.eventually.deep.include({
data: { data: {
logger: { logger: {
@ -17,11 +17,11 @@ describe('load config helper', async () => {
}) })
); );
it('should throw when config is missing', () => it('should throw when config is missing', async () =>
expect(loadConfig('missing.yml', join(__dirname, '..', 'docs'))).to.eventually.be.rejectedWith(NotFoundError) expect(loadConfig('missing.yml', join(__dirname, '..', 'docs'))).to.eventually.be.rejectedWith(NotFoundError)
); );
it('should load included config', () => it('should load included config', async () =>
expect(loadConfig('config-include.yml', join(__dirname, '..', 'docs'))).to.eventually.deep.include({ expect(loadConfig('config-include.yml', join(__dirname, '..', 'docs'))).to.eventually.deep.include({
data: { data: {
include: { include: {
@ -33,11 +33,11 @@ describe('load config helper', async () => {
}); });
describe('read config helper', async () => { describe('read config helper', async () => {
it('should consume enoent errors', () => it('should consume enoent errors', async () =>
expect(readConfig(join('docs', 'missing.yml'))).to.eventually.equal(undefined) expect(readConfig(join('docs', 'missing.yml'))).to.eventually.equal(undefined)
); );
it('should rethrow unknown errors', () => it('should rethrow unknown errors', async () =>
expect(readConfig('test')).to.eventually.be.rejectedWith(Error) expect(readConfig('test')).to.eventually.be.rejectedWith(Error)
); );
}); });

View File

@ -1,10 +1,10 @@
import { expect } from 'chai'; import { expect } from 'chai';
import { YamlParser } from '../../src/parser/YamlParser'; import { YamlParser } from '../../src/parser/YamlParser.js';
describe('yaml parser', () => { describe('yaml parser', () => {
describe('dump documents', () => { describe('dump documents', () => {
it('should dump multiple documents', () => { it('should dump multiple documents', async () => {
const parser = new YamlParser(); const parser = new YamlParser();
const data = parser.dump({}, {}); const data = parser.dump({}, {});
@ -13,7 +13,7 @@ describe('yaml parser', () => {
}); });
describe('parse documents', () => { describe('parse documents', () => {
it('should parse multiple documents', () => { it('should parse multiple documents', async () => {
const parser = new YamlParser(); const parser = new YamlParser();
const data = parser.parse(` const data = parser.parse(`
foo: {} foo: {}

View File

@ -3,10 +3,10 @@ import { vol } from 'memfs';
import { LogLevel, NullLogger } from 'noicejs'; import { LogLevel, NullLogger } from 'noicejs';
import { spy, stub } from 'sinon'; import { spy, stub } from 'sinon';
import { loadRuleFiles, loadRuleModules, loadRulePaths, loadRuleSource } from '../../src/rule'; import { loadRuleFiles, loadRuleModules, loadRulePaths, loadRuleSource } from '../../src/rule/index.js';
import { SchemaRule } from '../../src/rule/SchemaRule'; import { SchemaRule } from '../../src/rule/SchemaRule.js';
import { Filesystem, setFs } from '../../src/source'; import { Filesystem, setFs } from '../../src/source.js';
import { VisitorContext } from '../../src/visitor/VisitorContext'; import { VisitorContext } from '../../src/visitor/VisitorContext.js';
const EXAMPLE_EMPTY = '{name: foo, definitions: {}, rules: []}'; const EXAMPLE_EMPTY = '{name: foo, definitions: {}, rules: []}';
const EXAMPLE_RULES = `{ const EXAMPLE_RULES = `{

View File

@ -1,9 +1,9 @@
import { expect } from 'chai'; import { expect } from 'chai';
import { ConsoleLogger, LogLevel, NullLogger } from 'noicejs'; import { ConsoleLogger, LogLevel, NullLogger } from 'noicejs';
import { createRuleSelector, createRuleSources, resolveRules, validateRules } from '../../src/rule'; import { createRuleSelector, createRuleSources, resolveRules, validateRules } from '../../src/rule/index.js';
import { SchemaRule } from '../../src/rule/SchemaRule'; import { SchemaRule } from '../../src/rule/SchemaRule.js';
import { VisitorContext } from '../../src/visitor/VisitorContext'; import { VisitorContext } from '../../src/visitor/VisitorContext.js';
const TEST_RULES = [new SchemaRule({ const TEST_RULES = [new SchemaRule({
check: {}, check: {},
@ -118,7 +118,7 @@ describe('rule resolver', async () => {
}); });
describe('create rule sources helper', () => { describe('create rule sources helper', () => {
it('should ensure every field is an array', () => { it('should ensure every field is an array', async () => {
const sources = createRuleSources({}); const sources = createRuleSources({});
expect(sources).to.have.deep.property('ruleFile', []); expect(sources).to.have.deep.property('ruleFile', []);
@ -128,7 +128,7 @@ describe('create rule sources helper', () => {
}); });
describe('create rule selector helper', () => { describe('create rule selector helper', () => {
it('should ensure every field is an array', () => { it('should ensure every field is an array', async () => {
const sources = createRuleSelector({}); const sources = createRuleSelector({});
expect(sources).to.have.deep.property('excludeLevel', []); expect(sources).to.have.deep.property('excludeLevel', []);

View File

@ -2,9 +2,9 @@ import { expect } from 'chai';
import { LogLevel, NullLogger } from 'noicejs'; import { LogLevel, NullLogger } from 'noicejs';
import { mock, spy, stub } from 'sinon'; import { mock, spy, stub } from 'sinon';
import { RuleVisitor } from '../../src/rule/RuleVisitor'; import { RuleVisitor } from '../../src/rule/RuleVisitor.js';
import { SchemaRule } from '../../src/rule/SchemaRule'; import { SchemaRule } from '../../src/rule/SchemaRule.js';
import { VisitorContext } from '../../src/visitor/VisitorContext'; import { VisitorContext } from '../../src/visitor/VisitorContext.js';
describe('rule visitor', async () => { describe('rule visitor', async () => {
it('should only call visit for selected items', async () => { it('should only call visit for selected items', async () => {

View File

@ -2,8 +2,8 @@ import { expect } from 'chai';
import { LogLevel, NullLogger } from 'noicejs'; import { LogLevel, NullLogger } from 'noicejs';
import { stub } from 'sinon'; import { stub } from 'sinon';
import { friendlyError, SchemaRule } from '../../src/rule/SchemaRule'; import { friendlyError, SchemaRule } from '../../src/rule/SchemaRule.js';
import { VisitorContext } from '../../src/visitor/VisitorContext'; import { VisitorContext } from '../../src/visitor/VisitorContext.js';
/* eslint-disable @typescript-eslint/unbound-method */ /* eslint-disable @typescript-eslint/unbound-method */
@ -208,7 +208,7 @@ function createErrorContext() {
} }
describe('friendly errors', () => { describe('friendly errors', () => {
it('should have a message', () => { it('should have a message', async () => {
const { ctx } = createErrorContext(); const { ctx } = createErrorContext();
const err = friendlyError(ctx, { const err = friendlyError(ctx, {
instancePath: 'test-path', instancePath: 'test-path',
@ -219,7 +219,7 @@ describe('friendly errors', () => {
expect(err.msg).to.include(TEST_NAME); expect(err.msg).to.include(TEST_NAME);
}); });
it('should handle errors with an existing message', () => { it('should handle errors with an existing message', async () => {
const { ctx } = createErrorContext(); const { ctx } = createErrorContext();
const TEST_MESSAGE = 'test-message'; const TEST_MESSAGE = 'test-message';
const err = friendlyError(ctx, { const err = friendlyError(ctx, {

View File

@ -1,10 +1,10 @@
import { expect } from 'chai'; import { expect } from 'chai';
import { LogLevel, NullLogger } from 'noicejs'; import { LogLevel, NullLogger } from 'noicejs';
import { VisitorContext } from '../../src/visitor/VisitorContext'; import { VisitorContext } from '../../src/visitor/VisitorContext.js';
describe('visitor context', () => { describe('visitor context', () => {
it('should merge results', () => { it('should merge results', async () => {
const firstCtx = new VisitorContext({ const firstCtx = new VisitorContext({
logger: NullLogger.global, logger: NullLogger.global,
schemaOptions: { schemaOptions: {