Skip to content
Merged
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
13 changes: 9 additions & 4 deletions src/sonar/file-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function loadSchemaFiles(path: string, options: LoadSchemaFilesOptions =

return relevantPaths.map(path => {

if (path.includes('/index.') && options.ignoreIndex) {
if (isIndex(path, execOptions.extensions) && options.ignoreIndex) {
return false;
}

Expand Down Expand Up @@ -124,7 +124,7 @@ export function loadResolversFiles<Resolvers extends IResolvers = IResolvers>(pa

return relevantPaths.map(path => {

if (path.includes('/index.') && options.ignoreIndex) {
if (isIndex(path, execOptions.extensions) && options.ignoreIndex) {
return false;
}

Expand Down Expand Up @@ -155,7 +155,7 @@ export async function loadSchemaFilesAsync(path: string, options: LoadSchemaFile

return Promise.all(relevantPaths.map(async path => {

if (path.includes('/index.') && options.ignoreIndex) {
if (isIndex(path, execOptions.extensions) && options.ignoreIndex) {
return false;
}

Expand Down Expand Up @@ -191,7 +191,7 @@ export async function loadResolversFilesAsync<Resolvers extends IResolvers = IRe

return Promise.all(relevantPaths.map(async path => {

if (path.includes('/index.') && options.ignoreIndex) {
if (isIndex(path, execOptions.extensions) && options.ignoreIndex) {
return false;
}

Expand All @@ -204,3 +204,8 @@ export async function loadResolversFilesAsync<Resolvers extends IResolvers = IRe
}
}));
}

function isIndex(path: string, extensions: string[] = []): boolean {
const IS_INDEX = /(\/|\\)index\.[^\/\\]+$/i; // (/ or \) AND `index.` AND (everything except \ and /)(end of line)
return IS_INDEX.test(path) && extensions.some(ext => path.endsWith('.' + ext));
}
150 changes: 129 additions & 21 deletions tests/sonar/file-scanner.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import {loadResolversFiles, loadSchemaFiles} from '../../src';
import { loadResolversFiles, loadSchemaFiles } from '../../src';

function testSchemaDir(path: string, expectedResult: any, note: string, extensions?: string[] | null) {
function testSchemaDir({ path, expected, note, extensions, ignoreIndex }: { path: string; expected: any; note: string; extensions?: string[] | null; ignoreIndex?: boolean }) {
it(`should return the correct schema results for path: ${path} (${note})`, () => {
const result = loadSchemaFiles(path, extensions ? { extensions } : {});
const options = { ignoreIndex };
const result = loadSchemaFiles(path, extensions ? { ...options, extensions } : options);

expect(result.length).toBe(expectedResult.length);
expect(result.map(stripWhitespaces)).toEqual(expectedResult.map(stripWhitespaces));
expect(result.length).toBe(expected.length);
expect(result.map(stripWhitespaces)).toEqual(expected.map(stripWhitespaces));
});
}

function testResolversDir(path: string, expectedResult: any, note: string, extensions: string[] | null = null, compareValue = true) {
function testResolversDir({ path, expected, note, extensions, compareValue, ignoreIndex }: { path: string; expected: any; note: string; extensions?: string[]; compareValue?: boolean; ignoreIndex?: boolean }) {
if (typeof compareValue === 'undefined') {
compareValue = true;
}

it(`should return the correct resolvers results for path: ${path} (${note})`, () => {
const result = loadResolversFiles(path, extensions ? { extensions } : {});
const options = {
ignoreIndex,
};
const result = loadResolversFiles(path, extensions ? { ...options, extensions } : options);

expect(result.length).toBe(expectedResult.length);
expect(result.length).toBe(expected.length);

if (compareValue) {
expect(result).toEqual(expectedResult);
expect(result).toEqual(expected);
}
});
}
Expand All @@ -25,22 +33,122 @@ function stripWhitespaces(str: string): string {
return str.replace(/\s+/g, ' ').trim();
}

describe('file scanner', function () {
describe('file scanner', function() {
describe('schema', () => {
const schemaContent = `type MyType { f: String }`;
testSchemaDir('./tests/sonar/test-assets/1', [schemaContent], 'one file');
testSchemaDir('./tests/sonar/test-assets/2', [schemaContent, schemaContent, schemaContent], 'multiple files');
testSchemaDir('./tests/sonar/test-assets/3', [schemaContent, schemaContent, schemaContent], 'recursive');
testSchemaDir('./tests/sonar/test-assets/4', [schemaContent], 'custom extension', ['schema']);
testSchemaDir('./tests/sonar/test-assets/5', [schemaContent, schemaContent], 'custom extensions', ['schema', 'myschema']);
testSchemaDir('./tests/sonar/test-assets/10', [schemaContent, schemaContent, schemaContent], 'code files with gql tag', ['js']);
testSchemaDir({
path: './tests/sonar/test-assets/1',
expected: [schemaContent],
note: 'one file',
});
testSchemaDir({
path: './tests/sonar/test-assets/2',
expected: [schemaContent, schemaContent, schemaContent],
note: 'multiple files',
});
testSchemaDir({
path: './tests/sonar/test-assets/3',
expected: [schemaContent, schemaContent, schemaContent],
note: 'recursive',
});
testSchemaDir({
path: './tests/sonar/test-assets/4',
expected: [schemaContent],
note: 'custom extension',
extensions: ['schema'],
});
testSchemaDir({
path: './tests/sonar/test-assets/5',
expected: [schemaContent, schemaContent],
note: 'custom extensions',
extensions: ['schema', 'myschema'],
});
testSchemaDir({
path: './tests/sonar/test-assets/10',
expected: [schemaContent, schemaContent, schemaContent],
note: 'code files with gql tag',
extensions: ['js'],
});
testSchemaDir({
path: './tests/sonar/test-assets/10',
expected: [schemaContent, schemaContent, schemaContent],
note: 'code files with gql tag',
extensions: ['js'],
});
testSchemaDir({
path: './tests/sonar/test-assets/12',
expected: [schemaContent],
note: 'should ignore index on demand',
extensions: ['graphql'],
ignoreIndex: true,
});
testSchemaDir({
path: './tests/sonar/test-assets/12',
expected: [schemaContent, `type IndexType { f: Int }`],
note: 'should include index by default',
extensions: ['graphql'],
});
});

describe('resolvers', () => {
testResolversDir('./tests/sonar/test-assets/6', [{ MyType: { f: 1 }}], 'one file');
testResolversDir('./tests/sonar/test-assets/7', [{ MyType: { f: 1 }}, { MyType: { f: 2 }}], 'multiple files');
testResolversDir('./tests/sonar/test-assets/8', [{ MyType: { f: 1 }}], 'default export');
testResolversDir('./tests/sonar/test-assets/9', [{ MyType: { f: 1 }}, { MyType: { f: 2 }}], 'named exports');
testResolversDir('./tests/sonar/test-assets/11', (new Array(2)).fill(''), 'ignored extensions', null, false);
testResolversDir({
path: './tests/sonar/test-assets/6',
expected: [{ MyType: { f: 1 } }],
note: 'one file',
});
testResolversDir({
path: './tests/sonar/test-assets/7',
expected: [{ MyType: { f: 1 } }, { MyType: { f: 2 } }],
note: 'multiple files',
});
testResolversDir({
path: './tests/sonar/test-assets/8',
expected: [{ MyType: { f: 1 } }],
note: 'default export',
});
testResolversDir({
path: './tests/sonar/test-assets/9',
expected: [{ MyType: { f: 1 } }, { MyType: { f: 2 } }],
note: 'named exports',
});
testResolversDir({
path: './tests/sonar/test-assets/11',
expected: new Array(2).fill(''),
note: 'ignored extensions',
extensions: null,
compareValue: false,
});
testResolversDir({
path: './tests/sonar/test-assets/12',
expected: [
{
MyType: {
f: '12',
},
},
{
IndexType: {
f: '12',
},
},
],
note: 'includes index files but only if it matches extensions',
extensions: ['js'],
compareValue: true,
});
testResolversDir({
path: './tests/sonar/test-assets/12',
expected: [
{
MyType: {
f: '12',
},
},
],
note: 'ingore index files',
extensions: ['js'],
compareValue: true,
ignoreIndex: true,
});
});
});
3 changes: 3 additions & 0 deletions tests/sonar/test-assets/12/1.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type MyType {
f: String
}
13 changes: 13 additions & 0 deletions tests/sonar/test-assets/12/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const gql = require('graphql-tag');

const MyType = gql`
type MyType {
f: String
}
`;

module.exports = {
MyType: {
f: '12',
},
};
3 changes: 3 additions & 0 deletions tests/sonar/test-assets/12/index.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type IndexType {
f: Int
}
13 changes: 13 additions & 0 deletions tests/sonar/test-assets/12/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const gql = require('graphql-tag');

const IndexType = gql`
type IndexType {
f: Int
}
`;

module.exports = {
IndexType: {
f: '12',
},
};
5 changes: 5 additions & 0 deletions tests/sonar/test-assets/12/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
IngoredType: {
f: '12',
},
};