-
-
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.
- Loading branch information
1 parent
340ce63
commit 59a430f
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 { RubyLanguageDetector } from './RubyLanguageDetector'; | ||
|
||
describe('RubyLanguageDetector', () => { | ||
let detector: RubyLanguageDetector; | ||
let virtualFileSystemService: FileSystemService; | ||
|
||
beforeEach(() => { | ||
virtualFileSystemService = new FileSystemService({ isVirtual: true }); | ||
|
||
const fileInspector = new FileInspector(virtualFileSystemService, '/'); | ||
detector = new RubyLanguageDetector(fileInspector); | ||
}); | ||
|
||
afterEach(async () => { | ||
virtualFileSystemService.clearFileSystem(); | ||
}); | ||
|
||
it('detects ruby correctly via gemspec file', async () => { | ||
const structure: DirectoryJSON = { | ||
'/scan.gemspec': '...', | ||
}; | ||
|
||
virtualFileSystemService.setFileSystem(structure); | ||
|
||
const langAtPath = await detector.detectLanguage(); | ||
expect(langAtPath.length).toEqual(1); | ||
expect(langAtPath[0].language).toEqual(ProgrammingLanguage.Ruby); | ||
expect(langAtPath[0].path).toEqual(nodePath.sep); | ||
}); | ||
|
||
it("detects it's not a ruby 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 ruby correctly via rb file', async () => { | ||
const structure: DirectoryJSON = { | ||
'/index.rb': '...', | ||
}; | ||
|
||
virtualFileSystemService.setFileSystem(structure); | ||
|
||
const langAtPath = await detector.detectLanguage(); | ||
expect(langAtPath.length).toEqual(1); | ||
expect(langAtPath[0].language).toEqual(ProgrammingLanguage.Ruby); | ||
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 RubyLanguageDetector 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(fileExtensionRegExp(['gemspec']), '/'); | ||
|
||
if(packageFiles.length > 0) { | ||
for (const path of packageFiles.map((file) => nodePath.dirname(file.path))) { | ||
result.push({ language: ProgrammingLanguage.Ruby, path }); | ||
} | ||
} else { | ||
const rubyFiles = await this.fileInspector.scanFor(fileExtensionRegExp(['rb']), '/'); | ||
if (rubyFiles.length === 0) { | ||
return result; | ||
} | ||
const dirsWithProjects = uniq(rubyFiles.map((f) => nodePath.dirname(f.path))); | ||
// Get the shared subpath | ||
const commonPath = sharedSubpath(dirsWithProjects); | ||
result.push({ language: ProgrammingLanguage.Ruby, path: commonPath }); | ||
} | ||
return result; | ||
} | ||
} |