Skip to content

Commit

Permalink
feat: add golang language detector
Browse files Browse the repository at this point in the history
Fixes #82
  • Loading branch information
Kroustille committed Oct 17, 2019
1 parent 748c108 commit b063c3d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/detectors/Golang/GolangLanguageDetector.spec.ts
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);
});
});
37 changes: 37 additions & 0 deletions src/detectors/Golang/GolangLanguageDetector.ts
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;
}
}

0 comments on commit b063c3d

Please sign in to comment.