more tests for DockerfileService

This commit is contained in:
alecmerdler 2017-03-07 17:34:43 -08:00
parent 80b3666eb7
commit 32827d7ba4
3 changed files with 202 additions and 54 deletions

View file

@ -5,21 +5,32 @@ import { Injectable } from 'angular-ts-decorators';
@Injectable(DockerfileService.name)
export class DockerfileServiceImpl implements DockerfileService {
constructor(private DataFileService: any, private Config: any) {
console.log(`=================== DockerfileServiceImpl ==========================`);
constructor(private DataFileService: any, private Config: any, private FileReaderFactory: () => FileReader) {
}
public extractDockerfile(file: any): Promise<DockerfileInfoImpl | string> {
return new Promise((resolve, reject) => {
// TODO: Replace callbacks with promises
});
}
public getDockerfile(file: any,
success: (dockerfile: DockerfileInfoImpl) => void,
failure: (error: Event | string) => void): void {
var reader: FileReader = new FileReader();
reader.onload = (event: Event) => {
var dataArray: any = reader.result;
this.DataFileService.readDataArrayAsPossibleArchive(dataArray, (files) => {
this.processFiles(files, dataArray, success, failure);
}, () => {
var reader: FileReader = this.FileReaderFactory();
reader.onload = (event: any) => {
// FIXME: Debugging
console.log(event.target.result);
this.DataFileService.readDataArrayAsPossibleArchive(event.target.result,
(files: any[]) => {
this.processFiles(files, event.target.result, success, failure);
},
() => {
// Not an archive. Read directly as a single file.
this.processFiles([], dataArray, success, failure);
this.processFiles([], event.target.result, success, failure);
});
};
@ -27,13 +38,7 @@ export class DockerfileServiceImpl implements DockerfileService {
reader.readAsArrayBuffer(file);
}
public extractDockerfile(file: any): Promise<DockerfileInfoImpl | string> {
return new Promise((resolve, reject) => {
// TODO: Replace callbacks with promise
});
}
private processFiles(files: any,
private processFiles(files: any[],
dataArray: any[],
success: (dockerfile: DockerfileInfoImpl) => void,
failure: (error: ErrorEvent | string) => void): void {
@ -41,8 +46,8 @@ export class DockerfileServiceImpl implements DockerfileService {
// treat it as a single Dockerfile.
if (files.length == 0) {
this.DataFileService.arrayToString(dataArray, (contents: string) => {
var result = DockerfileInfoImpl.forData(contents, Object.assign({}, this.Config));
if (!result) {
var result: DockerfileInfoImpl | null = DockerfileInfoImpl.forData(contents, this.Config);
if (result == null) {
failure('File chosen is not a valid Dockerfile');
return;
}
@ -56,8 +61,8 @@ export class DockerfileServiceImpl implements DockerfileService {
files.forEach((file) => {
if (file['name'] == 'Dockerfile') {
this.DataFileService.blobToString(file.toBlob(), (contents: string) => {
var result: DockerfileInfoImpl | null = DockerfileInfoImpl.forData(contents, Object.assign({}, this.Config));
if (result != null) {
var result: DockerfileInfoImpl | null = DockerfileInfoImpl.forData(contents, this.Config);
if (result == null) {
failure('Dockerfile inside archive is not a valid Dockerfile');
return;
}
@ -104,7 +109,7 @@ export class DockerfileInfoImpl implements DockerfileInfo {
}
public getBaseImage(): string | null {
var imageAndTag = this.getBaseImageAndTag();
const imageAndTag = this.getBaseImageAndTag();
if (!imageAndTag) {
return null;
}
@ -114,15 +119,15 @@ export class DockerfileInfoImpl implements DockerfileInfo {
// 2) someimage:tag
// 3) host:port/someimage
// 4) host:port/someimage:tag
var lastIndex = imageAndTag.lastIndexOf(':');
if (lastIndex < 0) {
const lastIndex: number = imageAndTag.lastIndexOf(':');
if (lastIndex == -1) {
return imageAndTag;
}
// Otherwise, check if there is a / in the portion after the split point. If so,
// then the latter is part of the path (and not a tag).
var afterColon = imageAndTag.substring(lastIndex + 1);
if (afterColon.indexOf('/') >= 0) {
const afterColon: string = imageAndTag.substring(lastIndex + 1);
if (afterColon.indexOf('/') != -1) {
return imageAndTag;
}