-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { FileInspector } from '../../inspectors/FileInspector'; | ||
import { ProgrammingLanguage } from '../../model'; | ||
import { FileSystemService } from '../../services/FileSystemService'; | ||
import * as nodePath from 'path'; | ||
import { DirectoryJSON } from 'memfs/lib/volume'; | ||
import { GolangLanguageDetector } from './GolangLanguageDetector'; | ||
|
||
describe('GolangLanguageDetector', () => { | ||
let detector: GolangLanguageDetector; | ||
let virtualFileSystemService: FileSystemService; | ||
|
||
beforeEach(() => { | ||
virtualFileSystemService = new FileSystemService({ isVirtual: true }); | ||
|
||
const fileInspector = new FileInspector(virtualFileSystemService, '/'); | ||
detector = new GolangLanguageDetector(fileInspector); | ||
}); | ||
|
||
afterEach(async () => { | ||
virtualFileSystemService.clearFileSystem(); | ||
}); | ||
|
||
it('detects golang correctly via Gopkg.toml', async () => { | ||
const structure: DirectoryJSON = { | ||
'/Gopkg.toml': '...', | ||
}; | ||
|
||
virtualFileSystemService.setFileSystem(structure); | ||
|
||
const langAtPath = await detector.detectLanguage(); | ||
expect(langAtPath.length).toEqual(1); | ||
expect(langAtPath[0].language).toEqual(ProgrammingLanguage.Go); | ||
expect(langAtPath[0].path).toEqual(nodePath.sep); | ||
}); | ||
|
||
it("detects it's not a golang file", async () => { | ||
const structure: DirectoryJSON = { | ||
'/src/index.none': '...', | ||
}; | ||
|
||
virtualFileSystemService.setFileSystem(structure); | ||
|
||
const langAtPath = await detector.detectLanguage(); | ||
expect(langAtPath.length).toEqual(0); | ||
expect(langAtPath).toEqual([]); | ||
}); | ||
|
||
it('detects golang correctly via go file', async () => { | ||
const structure: DirectoryJSON = { | ||
'/index.go': '...', | ||
}; | ||
|
||
virtualFileSystemService.setFileSystem(structure); | ||
|
||
const langAtPath = await detector.detectLanguage(); | ||
expect(langAtPath.length).toEqual(1); | ||
expect(langAtPath[0].language).toEqual(ProgrammingLanguage.Go); | ||
expect(langAtPath[0].path).toEqual(nodePath.sep); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ILanguageDetector } from '../ILanguageDetector'; | ||
import { injectable, inject } from 'inversify'; | ||
import { LanguageAtPath, ProgrammingLanguage } from '../../model'; | ||
import { IFileInspector } from '../../inspectors/IFileInspector'; | ||
import { Types } from '../../types'; | ||
import { fileNameRegExp, fileExtensionRegExp, sharedSubpath } from '../utils'; | ||
import { uniq } from 'lodash'; | ||
import * as nodePath from 'path'; | ||
|
||
@injectable() | ||
export class GolangLanguageDetector implements ILanguageDetector { | ||
private fileInspector: IFileInspector; | ||
constructor(@inject(Types.IFileInspector) fileInspector: IFileInspector) { | ||
this.fileInspector = fileInspector; | ||
} | ||
|
||
async detectLanguage(): Promise<LanguageAtPath[]> { | ||
const result: LanguageAtPath[] = []; | ||
const packageFiles = await this.fileInspector.scanFor(fileNameRegExp('Gopkg.toml'), '/'); | ||
|
||
if(packageFiles.length > 0) { | ||
for (const path of packageFiles.map((file) => nodePath.dirname(file.path))) { | ||
result.push({ language: ProgrammingLanguage.Go, path }); | ||
} | ||
} else { | ||
const goFiles = await this.fileInspector.scanFor(fileExtensionRegExp(['go']), '/'); | ||
if (goFiles.length === 0) { | ||
return result; | ||
} | ||
const dirsWithProjects = uniq(goFiles.map((f) => nodePath.dirname(f.path))); | ||
// Get the shared subpath | ||
const commonPath = sharedSubpath(dirsWithProjects); | ||
result.push({ language: ProgrammingLanguage.Go, path: commonPath }); | ||
} | ||
return result; | ||
} | ||
} |