@@ -9,10 +9,9 @@ import * as fs from 'fs';
99import * as path from 'path' ;
1010import * as os from 'os' ;
1111import { CodeGraph } from '../src' ;
12- import { extractFromSource , scanDirectory , shouldIncludeFile } from '../src/extraction' ;
12+ import { extractFromSource , scanDirectory } from '../src/extraction' ;
1313import { detectLanguage , isLanguageSupported , getSupportedLanguages , initGrammars , loadAllGrammars } from '../src/extraction/grammars' ;
1414import { normalizePath } from '../src/utils' ;
15- import { DEFAULT_CONFIG } from '../src/types' ;
1615
1716beforeAll ( async ( ) => {
1817 await initGrammars ( ) ;
@@ -3003,48 +3002,65 @@ describe('Directory Exclusion', () => {
30033002 cleanupTempDir ( tempDir ) ;
30043003 } ) ;
30053004
3006- it ( 'should exclude node_modules directories' , ( ) => {
3007- // Create structure: src/index.ts + node_modules/pkg/index.js
3005+ it ( 'should exclude directories listed in .gitignore ' , ( ) => {
3006+ // Create structure: src/index.ts + node_modules/pkg/index.js, gitignore node_modules
30083007 const srcDir = path . join ( tempDir , 'src' ) ;
30093008 const nmDir = path . join ( tempDir , 'node_modules' , 'pkg' ) ;
30103009 fs . mkdirSync ( srcDir , { recursive : true } ) ;
30113010 fs . mkdirSync ( nmDir , { recursive : true } ) ;
30123011 fs . writeFileSync ( path . join ( srcDir , 'index.ts' ) , 'export const x = 1;' ) ;
30133012 fs . writeFileSync ( path . join ( nmDir , 'index.js' ) , 'module.exports = {};' ) ;
3013+ fs . writeFileSync ( path . join ( tempDir , '.gitignore' ) , 'node_modules/\n' ) ;
30143014
3015- const config = { ...DEFAULT_CONFIG , rootDir : tempDir } ;
3016- const files = scanDirectory ( tempDir , config ) ;
3015+ const files = scanDirectory ( tempDir ) ;
30173016
30183017 expect ( files ) . toContain ( 'src/index.ts' ) ;
30193018 expect ( files . every ( ( f ) => ! f . includes ( 'node_modules' ) ) ) . toBe ( true ) ;
30203019 } ) ;
30213020
3022- it ( 'should exclude nested node_modules directories ' , ( ) => {
3023- // Create structure: packages/app/node_modules/pkg/index.js
3021+ it ( 'should exclude nested node_modules via a root .gitignore ' , ( ) => {
3022+ // A trailing-slash pattern with no leading slash matches at any depth.
30243023 const srcDir = path . join ( tempDir , 'packages' , 'app' , 'src' ) ;
30253024 const nmDir = path . join ( tempDir , 'packages' , 'app' , 'node_modules' , 'pkg' ) ;
30263025 fs . mkdirSync ( srcDir , { recursive : true } ) ;
30273026 fs . mkdirSync ( nmDir , { recursive : true } ) ;
30283027 fs . writeFileSync ( path . join ( srcDir , 'index.ts' ) , 'export const x = 1;' ) ;
30293028 fs . writeFileSync ( path . join ( nmDir , 'index.js' ) , 'module.exports = {};' ) ;
3029+ fs . writeFileSync ( path . join ( tempDir , '.gitignore' ) , 'node_modules/\n' ) ;
30303030
3031- const config = { ...DEFAULT_CONFIG , rootDir : tempDir } ;
3032- const files = scanDirectory ( tempDir , config ) ;
3031+ const files = scanDirectory ( tempDir ) ;
30333032
30343033 expect ( files ) . toContain ( 'packages/app/src/index.ts' ) ;
30353034 expect ( files . every ( ( f ) => ! f . includes ( 'node_modules' ) ) ) . toBe ( true ) ;
30363035 } ) ;
30373036
3038- it ( 'should exclude .git directories' , ( ) => {
3037+ it ( 'should apply a nested .gitignore only to its own subtree' , ( ) => {
3038+ const appSrc = path . join ( tempDir , 'app' , 'src' ) ;
3039+ fs . mkdirSync ( appSrc , { recursive : true } ) ;
3040+ fs . writeFileSync ( path . join ( appSrc , 'keep.ts' ) , 'export const a = 1;' ) ;
3041+ fs . writeFileSync ( path . join ( appSrc , 'skip.ts' ) , 'export const b = 2;' ) ;
3042+ fs . writeFileSync ( path . join ( tempDir , 'app' , '.gitignore' ) , 'src/skip.ts\n' ) ;
3043+ // A sibling with the same name outside app/ must NOT be ignored.
3044+ const otherDir = path . join ( tempDir , 'other' , 'src' ) ;
3045+ fs . mkdirSync ( otherDir , { recursive : true } ) ;
3046+ fs . writeFileSync ( path . join ( otherDir , 'skip.ts' ) , 'export const c = 3;' ) ;
3047+
3048+ const files = scanDirectory ( tempDir ) ;
3049+
3050+ expect ( files ) . toContain ( 'app/src/keep.ts' ) ;
3051+ expect ( files ) . not . toContain ( 'app/src/skip.ts' ) ;
3052+ expect ( files ) . toContain ( 'other/src/skip.ts' ) ;
3053+ } ) ;
3054+
3055+ it ( 'should always skip .git directories' , ( ) => {
30393056 const srcDir = path . join ( tempDir , 'src' ) ;
30403057 const gitDir = path . join ( tempDir , '.git' , 'objects' ) ;
30413058 fs . mkdirSync ( srcDir , { recursive : true } ) ;
30423059 fs . mkdirSync ( gitDir , { recursive : true } ) ;
30433060 fs . writeFileSync ( path . join ( srcDir , 'index.ts' ) , 'export const x = 1;' ) ;
30443061 fs . writeFileSync ( path . join ( gitDir , 'pack.ts' ) , 'export const y = 2;' ) ;
30453062
3046- const config = { ...DEFAULT_CONFIG , rootDir : tempDir } ;
3047- const files = scanDirectory ( tempDir , config ) ;
3063+ const files = scanDirectory ( tempDir ) ;
30483064
30493065 expect ( files ) . toContain ( 'src/index.ts' ) ;
30503066 expect ( files . every ( ( f ) => ! f . includes ( '.git' ) ) ) . toBe ( true ) ;
@@ -3055,29 +3071,12 @@ describe('Directory Exclusion', () => {
30553071 fs . mkdirSync ( srcDir , { recursive : true } ) ;
30563072 fs . writeFileSync ( path . join ( srcDir , 'Button.tsx' ) , 'export function Button() {}' ) ;
30573073
3058- const config = { ...DEFAULT_CONFIG , rootDir : tempDir } ;
3059- const files = scanDirectory ( tempDir , config ) ;
3074+ const files = scanDirectory ( tempDir ) ;
30603075
30613076 expect ( files . length ) . toBe ( 1 ) ;
30623077 expect ( files [ 0 ] ) . toBe ( 'src/components/Button.tsx' ) ;
30633078 expect ( files [ 0 ] ) . not . toContain ( '\\' ) ;
30643079 } ) ;
3065-
3066- it ( 'should respect .codegraphignore marker' , ( ) => {
3067- const srcDir = path . join ( tempDir , 'src' ) ;
3068- const vendorDir = path . join ( tempDir , 'vendor' ) ;
3069- fs . mkdirSync ( srcDir , { recursive : true } ) ;
3070- fs . mkdirSync ( vendorDir , { recursive : true } ) ;
3071- fs . writeFileSync ( path . join ( srcDir , 'index.ts' ) , 'export const x = 1;' ) ;
3072- fs . writeFileSync ( path . join ( vendorDir , 'lib.ts' ) , 'export const y = 2;' ) ;
3073- fs . writeFileSync ( path . join ( vendorDir , '.codegraphignore' ) , '' ) ;
3074-
3075- const config = { ...DEFAULT_CONFIG , rootDir : tempDir } ;
3076- const files = scanDirectory ( tempDir , config ) ;
3077-
3078- expect ( files ) . toContain ( 'src/index.ts' ) ;
3079- expect ( files . every ( ( f ) => ! f . includes ( 'vendor' ) ) ) . toBe ( true ) ;
3080- } ) ;
30813080} ) ;
30823081
30833082describe ( 'Git Submodules' , ( ) => {
@@ -3124,8 +3123,7 @@ describe('Git Submodules', () => {
31243123 ) ;
31253124 git ( mainDir , 'commit' , '-q' , '-m' , 'add submodule' ) ;
31263125
3127- const config = { ...DEFAULT_CONFIG , rootDir : mainDir } ;
3128- const files = scanDirectory ( mainDir , config ) ;
3126+ const files = scanDirectory ( mainDir ) ;
31293127
31303128 expect ( files ) . toContain ( 'app.ts' ) ;
31313129 expect ( files ) . toContain ( 'libs/lib/lib.ts' ) ;
@@ -3173,8 +3171,7 @@ describe('Nested non-submodule git repos', () => {
31733171 git ( path . join ( root , 'sub_repo2' ) , 'init' , '-q' ) ;
31743172 fs . writeFileSync ( path . join ( sub2 , 'two.ts' ) , 'export const two = 2;' ) ;
31753173
3176- const config = { ...DEFAULT_CONFIG , rootDir : root } ;
3177- const files = scanDirectory ( root , config ) ;
3174+ const files = scanDirectory ( root ) ;
31783175
31793176 // Both committed and untracked source from the nested repos must be found.
31803177 expect ( files ) . toContain ( 'sub_repo1/src/one.ts' ) ;
@@ -3197,8 +3194,7 @@ describe('Nested non-submodule git repos', () => {
31973194 fs . writeFileSync ( path . join ( sub , 'real.ts' ) , 'export const real = 1;' ) ;
31983195 fs . writeFileSync ( path . join ( sub , 'generated.ts' ) , 'export const generated = 1;' ) ;
31993196
3200- const config = { ...DEFAULT_CONFIG , rootDir : root } ;
3201- const files = scanDirectory ( root , config ) ;
3197+ const files = scanDirectory ( root ) ;
32023198
32033199 expect ( files ) . toContain ( 'sub_repo/src/real.ts' ) ;
32043200 expect ( files ) . not . toContain ( 'sub_repo/src/generated.ts' ) ;
0 commit comments