Skip to content
Draft
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
84 changes: 84 additions & 0 deletions core/aws-lsp-core/src/util/gitIgnoreFilter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import * as assert from 'assert'
import * as path from 'path'
import { Workspace } from '@aws/language-server-runtimes/server-interface'
import { GitIgnoreFilter } from './gitIgnoreFilter'

describe('GitIgnoreFilter', () => {
const rootPath = path.resolve('gitignore-filter-root')
const inRoot = (...segments: string[]) => path.join(rootPath, ...segments)

async function filter(gitIgnoreFiles: Record<string, string>, files: string[]): Promise<string[]> {
const workspace = {
fs: {
readFile: async (filePath: string) => {
const content = gitIgnoreFiles[filePath]
if (content === undefined) {
throw new Error(`unexpected read of ${filePath}`)
}
return content
},
},
} as unknown as Workspace
const gitIgnoreFilter = await GitIgnoreFilter.build(rootPath, Object.keys(gitIgnoreFiles), workspace)
return gitIgnoreFilter.filterFiles(files)
}

it('applies the patterns of the root .gitignore file to the whole tree', async () => {
const kept = await filter({ [inRoot('.gitignore')]: 'build/\n*.log\n' }, [
inRoot('src', 'a.cs'),
inRoot('build', 'a.cs'),
inRoot('a.log'),
inRoot('sub', 'b.log'),
])

assert.deepStrictEqual(kept, [inRoot('src', 'a.cs')])
})

it('applies an anchored pattern relative to the folder of its .gitignore file', async () => {
const kept = await filter({ [inRoot('sub', '.gitignore')]: '/x.cs\n' }, [
inRoot('x.cs'),
inRoot('sub', 'x.cs'),
inRoot('sub', 'deeper', 'x.cs'),
])

assert.deepStrictEqual(kept, [inRoot('x.cs'), inRoot('sub', 'deeper', 'x.cs')])
})

it('applies the patterns of a nested .gitignore file only inside its folder', async () => {
const kept = await filter({ [inRoot('sub', '.gitignore')]: '*.tmp\nout/\n' }, [
inRoot('a.tmp'),
inRoot('other', 'a.tmp'),
inRoot('sub2', 'a.tmp'),
inRoot('out', 'a.cs'),
inRoot('sub', 'a.tmp'),
inRoot('sub', 'deeper', 'b.tmp'),
inRoot('sub', 'out', 'a.cs'),
inRoot('sub', 'a.cs'),
])

assert.deepStrictEqual(kept, [
inRoot('a.tmp'),
inRoot('other', 'a.tmp'),
inRoot('sub2', 'a.tmp'),
inRoot('out', 'a.cs'),
inRoot('sub', 'a.cs'),
])
})

it('keeps a file only when every .gitignore file that covers it accepts it', async () => {
const kept = await filter(
{
[inRoot('.gitignore')]: '*.log\n',
[inRoot('sub', '.gitignore')]: '/generated/\n',
},
[
inRoot('generated', 'a.cs'),
inRoot('sub', 'generated', 'a.cs'),
inRoot('sub', 'a.log'),
inRoot('sub', 'a.cs'),
]
)

assert.deepStrictEqual(kept, [inRoot('generated', 'a.cs'), inRoot('sub', 'a.cs')])
})
})
9 changes: 8 additions & 1 deletion core/aws-lsp-core/src/util/gitIgnoreFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export class GitIgnoreFilter {
this.acceptors = acceptors
}

/**
* @param rootPath Root folder of the scan. Not used: each .gitignore file applies to the folder that contains
* it. The parameter stays so that existing callers keep working.
* @param gitIgnoreFiles Paths of the .gitignore files to apply.
*/
public static async build(
rootPath: string,
gitIgnoreFiles: string[],
Expand All @@ -27,8 +32,10 @@ export class GitIgnoreFilter {
const fileContent = await workspace.fs.readFile(file)
const gitIgnoreAcceptor = parser.compile(fileContent)

// As in Git, the patterns of a .gitignore file apply relative to the folder that contains it. Unlike Git, a
// pattern in a nested file cannot include again a file that a .gitignore file above it excludes.
acceptors.push({
folderPath: rootPath,
folderPath: path.dirname(file),
acceptor: gitIgnoreAcceptor,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,25 @@ describe('Test CsharpDependencyGraph', () => {
await csharpDependencyGraph.createNamespaceFilenameMapper(projectPathUri)
assert.deepStrictEqual(csharpDependencyGraph.namespaceToFilepathDirectory, new Map([]))
})
it('should treat the dot between namespace names as a literal dot', async () => {
csharpDependencyGraph.namespaceToFilepathDirectory = new Map<string, Set<string>>()
mockedFs.readFile.callsFake(async filePath => {
if (filePath === path.join(projectPathUri, 'src', 'sample.cs')) {
return `namespace Foo.Bar {}`
}
if (filePath === path.join(projectPathUri, 'src', 'model.cs')) {
return `namespace Foo Bar {}`
}
})
await csharpDependencyGraph.createNamespaceFilenameMapper(projectPathUri)
assert.deepStrictEqual(
csharpDependencyGraph.namespaceToFilepathDirectory,
new Map([
['Foo.Bar', new Set([path.join(projectPathUri, 'src', 'sample.cs')])],
['Foo', new Set([path.join(projectPathUri, 'src', 'model.cs')])],
])
)
})
})
describe('Test searchDependency', () => {
beforeEach(() => {
Expand Down Expand Up @@ -382,6 +401,43 @@ namespace Amazon.Toolkit.Demo {
const response = csharpDependencyGraph.readImports(content)
assert.deepStrictEqual(response, [])
})
it('should read indented, aliased and static imports', () => {
const content = [
' using System.Collections.Generic;',
'\tglobal using static System.Math;',
'using Alias = Foo.Bar.Baz;',
'using X;',
].join('\r\n')
assert.deepStrictEqual(csharpDependencyGraph.readImports(content), [
'System.Collections.Generic',
'System.Math',
'Foo.Bar.Baz',
'X',
])
})
it('should treat the dot between names as a literal dot', () => {
const content = ['using Foo Bar;', 'using Foo,Bar;', 'using Foo-Bar;', 'using Foo.Bar;'].join('\n')
assert.deepStrictEqual(csharpDependencyGraph.readImports(content), ['Foo.Bar'])
})

// Each case must finish within the limit. Each line takes well under a millisecond, so the limit only has to
// catch a regression, not measure speed.
const largeInputs: { name: string; content: string }[] = [
{ name: 'a long name', content: `using ${'A'.repeat(40)}` },
{ name: 'a long aliased name', content: `using Alias = ${'A'.repeat(40)}` },
{ name: 'a long dotted name', content: `using ${Array(5000).fill('Ab').join('.')}` },
{ name: 'a long run of whitespace', content: `${' '.repeat(100000)}x` },
]
for (const { name, content } of largeInputs) {
it(`should read ${name} within the time limit`, () => {
const start = process.hrtime.bigint()
const response = csharpDependencyGraph.readImports(content)
const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6

assert.deepStrictEqual(response, [])
assert.ok(elapsedMs < 500, `readImports took ${elapsedMs.toFixed(1)} ms`)
})
}
})
describe('Test generateTruncation', () => {
before(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import * as CodeWhispererConstants from './constants'
import { DependencyGraph, Truncation } from './dependencyGraph'
import path = require('path')

export const importRegex = /((global\s+)?using\s+(static\s+)?)([A-Z]\w*\s*?=\s*)?([A-Z]\w*(.[A-Z]\w*)*);/gm
// A '.' between two names is matched as a literal dot, not as any character.
export const importRegex = /((global\s+)?using\s+(static\s+)?)([A-Z]\w*\s*?=\s*)?([A-Z]\w*(\.[A-Z]\w*)*);/gm

export class CsharpDependencyGraph extends DependencyGraph {
// This contains a dictionary of namespace name and the set of filepaths, where namespace has been defined.
Expand All @@ -24,7 +25,7 @@ export class CsharpDependencyGraph extends DependencyGraph {

const csharpFiles = await this.filterFiles(workspacePath, files)

const searchRegEx = new RegExp('namespace ([A-Z]\\w*(.[A-Z]\\w*)*)', 'g')
const searchRegEx = new RegExp('namespace ([A-Z]\\w*(\\.[A-Z]\\w*)*)', 'g')
for (const filePath of csharpFiles) {
const content = await this.workspace.fs.readFile(filePath)
if (!content) {
Expand Down Expand Up @@ -113,12 +114,8 @@ export class CsharpDependencyGraph extends DependencyGraph {
readImports(content: string) {
const lineBreakRegex = new RegExp('\\r?\\n', 'g')
this._totalLines += content.split(lineBreakRegex).length
const importNamespaceRegex = new RegExp(
'(\\s*(global\\s+)?using\\s+(static\\s+)?)([A-Z]\\w*\\s*?=\\s*)?([A-Z]\\w*(.[A-Z]\\w*)*);',
'g'
)
const imports: string[] = []
for (const matchedStr of content.matchAll(importNamespaceRegex)) {
for (const matchedStr of content.matchAll(importRegex)) {
imports.push(matchedStr[5])
}
return imports
Expand Down
Loading