-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(stryker-init): Stryker init won't create temp folder (#361)
* Refactor `StrykerTempFolder` to a class using a singleton * Add `initialize` method for the actual initialization of the folder. * Rename `StrykerTempFolder` -> `TempFolder` * Add unit tests for TempFolder
- Loading branch information
1 parent
be2cc17
commit a4333c9
Showing
11 changed files
with
243 additions
and
111 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
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
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
This file was deleted.
Oops, something went wrong.
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,95 @@ | ||
import * as fs from 'mz/fs'; | ||
import * as path from 'path'; | ||
import * as mkdirp from 'mkdirp'; | ||
import * as log4js from 'log4js'; | ||
import { deleteDir } from './fileUtils'; | ||
|
||
const log = log4js.getLogger('TempFolder'); | ||
|
||
export class TempFolder { | ||
baseTempFolder: string; | ||
tempFolder: string; | ||
|
||
private constructor() { } | ||
|
||
initialize(tempDirName = '.stryker-tmp') { | ||
this.baseTempFolder = path.join(process.cwd(), tempDirName); | ||
this.tempFolder = path.join(this.baseTempFolder, this.random().toString()); | ||
mkdirp.sync(this.baseTempFolder); | ||
mkdirp.sync(this.tempFolder); | ||
} | ||
|
||
/** | ||
* Creates a new random folder with the specified prefix. | ||
* @param prefix The prefix. | ||
* @returns The path to the folder. | ||
*/ | ||
createRandomFolder(prefix: string): string { | ||
if (!this.baseTempFolder) { | ||
throw new Error('initialize() was not called!'); | ||
} | ||
let dir = this.baseTempFolder + path.sep + prefix + this.random(); | ||
mkdirp.sync(dir); | ||
return dir; | ||
} | ||
|
||
/** | ||
* Writes data to a specified file. | ||
* @param filename The path to the file. | ||
* @param data The content of the file. | ||
* @returns A promise to eventually save the file. | ||
*/ | ||
writeFile(filename: string, data: string): Promise<void> { | ||
return fs.writeFile(filename, data, { encoding: 'utf8' }); | ||
} | ||
|
||
/** | ||
* Copies a file. | ||
* @param fromFilename The path to the existing file. | ||
* @param toFilename The path to copy the file to. | ||
* @param instrumenter An optional additional instrumenter to stream the file through | ||
* @returns A promise to eventually copy the file. | ||
*/ | ||
copyFile(fromFilename: string, toFilename: string, instrumenter: NodeJS.ReadWriteStream | null): Promise<void> { | ||
return new Promise<void>((resolve, reject) => { | ||
let readStream: NodeJS.ReadableStream = fs.createReadStream(fromFilename, { encoding: 'utf8' }); | ||
let writeStream = fs.createWriteStream(toFilename); | ||
readStream.on('error', reject); | ||
writeStream.on('error', reject); | ||
if (instrumenter) { | ||
readStream = readStream.pipe(instrumenter); | ||
} | ||
readStream.pipe(writeStream); | ||
readStream.on('end', () => resolve()); | ||
}); | ||
} | ||
|
||
/** | ||
* Deletes the Stryker-temp folder | ||
*/ | ||
clean() { | ||
if (!this.baseTempFolder) { | ||
throw new Error('initialize() was not called!'); | ||
} | ||
log.debug(`Deleting stryker temp folder ${this.baseTempFolder}`); | ||
return deleteDir(this.baseTempFolder) | ||
.catch(() => log.info(`Failed to delete stryker temp folder ${this.baseTempFolder}`)); | ||
} | ||
|
||
/** | ||
* Creates a random integer number. | ||
* @returns A random integer. | ||
*/ | ||
random(): number { | ||
return Math.ceil(Math.random() * 10000000); | ||
} | ||
|
||
private static _instance: TempFolder; | ||
static instance() { | ||
if (!this._instance) { | ||
this._instance = new TempFolder(); | ||
} | ||
return this._instance; | ||
} | ||
} | ||
|
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
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
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
Oops, something went wrong.