diff --git a/src/sonar/file-scanner.ts b/src/sonar/file-scanner.ts index fa769a82..03116135 100644 --- a/src/sonar/file-scanner.ts +++ b/src/sonar/file-scanner.ts @@ -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; } @@ -124,7 +124,7 @@ export function loadResolversFiles(pa return relevantPaths.map(path => { - if (path.includes('/index.') && options.ignoreIndex) { + if (isIndex(path, execOptions.extensions) && options.ignoreIndex) { return false; } @@ -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; } @@ -191,7 +191,7 @@ export async function loadResolversFilesAsync { - if (path.includes('/index.') && options.ignoreIndex) { + if (isIndex(path, execOptions.extensions) && options.ignoreIndex) { return false; } @@ -204,3 +204,8 @@ export async function loadResolversFilesAsync path.endsWith('.' + ext)); +} diff --git a/tests/sonar/file-scanner.spec.ts b/tests/sonar/file-scanner.spec.ts index 843f3d53..9de04d83 100644 --- a/tests/sonar/file-scanner.spec.ts +++ b/tests/sonar/file-scanner.spec.ts @@ -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); } }); } @@ -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, + }); }); }); diff --git a/tests/sonar/test-assets/12/1.graphql b/tests/sonar/test-assets/12/1.graphql new file mode 100644 index 00000000..2e757754 --- /dev/null +++ b/tests/sonar/test-assets/12/1.graphql @@ -0,0 +1,3 @@ +type MyType { + f: String +} diff --git a/tests/sonar/test-assets/12/1.js b/tests/sonar/test-assets/12/1.js new file mode 100644 index 00000000..7507c912 --- /dev/null +++ b/tests/sonar/test-assets/12/1.js @@ -0,0 +1,13 @@ +const gql = require('graphql-tag'); + +const MyType = gql` + type MyType { + f: String + } +`; + +module.exports = { + MyType: { + f: '12', + }, +}; diff --git a/tests/sonar/test-assets/12/index.graphql b/tests/sonar/test-assets/12/index.graphql new file mode 100644 index 00000000..52a0c2c3 --- /dev/null +++ b/tests/sonar/test-assets/12/index.graphql @@ -0,0 +1,3 @@ +type IndexType { + f: Int +} diff --git a/tests/sonar/test-assets/12/index.js b/tests/sonar/test-assets/12/index.js new file mode 100644 index 00000000..b562e82b --- /dev/null +++ b/tests/sonar/test-assets/12/index.js @@ -0,0 +1,13 @@ +const gql = require('graphql-tag'); + +const IndexType = gql` + type IndexType { + f: Int + } +`; + +module.exports = { + IndexType: { + f: '12', + }, +}; diff --git a/tests/sonar/test-assets/12/index.ts b/tests/sonar/test-assets/12/index.ts new file mode 100644 index 00000000..ac68c5f4 --- /dev/null +++ b/tests/sonar/test-assets/12/index.ts @@ -0,0 +1,5 @@ +export default { + IngoredType: { + f: '12', + }, +};