Skip to content

Commit c9029c2

Browse files
HSARBogdan Gavril
authored andcommitted
Code review comments for Maven and Gradle file system interaction improvements.
1 parent c2e7fea commit c9029c2

File tree

3 files changed

+275
-14
lines changed

3 files changed

+275
-14
lines changed

Tasks/Gradle/CodeAnalysis/Common/FileSystemInteractions.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import fs = require('fs');
55
import tl = require('vsts-task-lib/task');
66

77
export class FileSystemInteractions {
8+
9+
/**
10+
* Copies a file from the source path to the destination path.
11+
* If the destination path already exists, it is replaced. Otherwise a new file is created.
12+
*
13+
* Adapted from: https://github.com/Microsoft/vsts-task-lib/blob/master/node/task.ts
14+
* @param sourcePath Path to copy from
15+
* @param destinationPath Path to copy to
16+
*/
817
public static copyFile(sourcePath:string, destinationPath:string):void {
918
shell.cp('-f', sourcePath, destinationPath);
1019

@@ -13,7 +22,9 @@ export class FileSystemInteractions {
1322

1423
/**
1524
* Create a directory at the specified path, including any folders in between.
16-
* @param directoryPath Path to create.
25+
*
26+
* Copied from: https://github.com/Microsoft/vsts-task-lib/blob/master/node/task.ts
27+
* @param directoryPath Path to create
1728
*/
1829
public static createDirectory(directoryPath:string):void {
1930
// build a stack of directories to create
@@ -72,7 +83,14 @@ export class FileSystemInteractions {
7283
}
7384
}
7485

75-
public static checkShell(cmd: string, continueOnError?: boolean) {
86+
/**
87+
* Checks that no errors were produced by the previous command.
88+
*
89+
* Copied from: https://github.com/Microsoft/vsts-task-lib/blob/master/node/task.ts
90+
* @param cmd Command issued
91+
* @param continueOnError Do not throw an exception if an error was produced
92+
*/
93+
private static checkShell(cmd: string, continueOnError?: boolean) {
7694
var se = shell.error();
7795

7896
if (se) {

Tasks/Maven/CodeAnalysis/Common/FileSystemInteractions.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import fs = require('fs');
55
import tl = require('vsts-task-lib/task');
66

77
export class FileSystemInteractions {
8+
9+
/**
10+
* Copies a file from the source path to the destination path.
11+
* If the destination path already exists, it is replaced. Otherwise a new file is created.
12+
*
13+
* Adapted from: https://github.com/Microsoft/vsts-task-lib/blob/master/node/task.ts
14+
* @param sourcePath Path to copy from
15+
* @param destinationPath Path to copy to
16+
*/
817
public static copyFile(sourcePath:string, destinationPath:string):void {
918
shell.cp('-f', sourcePath, destinationPath);
1019

@@ -13,7 +22,9 @@ export class FileSystemInteractions {
1322

1423
/**
1524
* Create a directory at the specified path, including any folders in between.
16-
* @param directoryPath Path to create.
25+
*
26+
* Copied from: https://github.com/Microsoft/vsts-task-lib/blob/master/node/task.ts
27+
* @param directoryPath Path to create
1728
*/
1829
public static createDirectory(directoryPath:string):void {
1930
// build a stack of directories to create
@@ -72,7 +83,14 @@ export class FileSystemInteractions {
7283
}
7384
}
7485

75-
public static checkShell(cmd: string, continueOnError?: boolean) {
86+
/**
87+
* Checks that no errors were produced by the previous command.
88+
*
89+
* Copied from: https://github.com/Microsoft/vsts-task-lib/blob/master/node/task.ts
90+
* @param cmd Command issued
91+
* @param continueOnError Do not throw an exception if an error was produced
92+
*/
93+
private static checkShell(cmd: string, continueOnError?: boolean) {
7694
var se = shell.error();
7795

7896
if (se) {

Tests/L0/Maven/_suite.ts

Lines changed: 235 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import path = require('path');
88
import fs = require('fs');
99
import url = require('url');
1010
import {Url} from 'url';
11+
import shell = require('shelljs');
1112

1213
import {ToolRunner} from 'vsts-task-lib/toolrunner';
1314
import tr = require('../../lib/vsts-task-lib/toolRunner');
@@ -23,6 +24,8 @@ import {SonarQubeMetrics} from '../../../Tasks/Maven/CodeAnalysis/SonarQube/metr
2324
import {SonarQubeMeasurementUnit} from '../../../Tasks/Maven/CodeAnalysis/SonarQube/metrics';
2425
import {MockSonarQubeServer} from './server-mock';
2526

27+
import {FileSystemInteractions} from '../../../Tasks/Maven/CodeAnalysis/Common/FileSystemInteractions';
28+
2629
import http = require('http');
2730
import {IncomingMessage} from 'http';
2831

@@ -97,12 +100,7 @@ function setupMockResponsesForPaths(responseObject: any, paths: string[]) { // C
97100

98101
// Create temp dirs for mavencodeanalysis tests to save into
99102
function createTempDirsForCodeAnalysisTests(): void {
100-
var testTempDir: string = path.join(__dirname, '_temp');
101-
var caTempDir: string = path.join(testTempDir, '.codeAnalysis');
102-
103-
if (!fs.existsSync(testTempDir)) {
104-
fs.mkdirSync(testTempDir);
105-
}
103+
var caTempDir: string = path.join(createTempDir(), '.codeAnalysis');
106104

107105
if (!fs.existsSync(caTempDir)) {
108106
fs.mkdirSync(caTempDir);
@@ -111,16 +109,21 @@ function createTempDirsForCodeAnalysisTests(): void {
111109

112110
// Create temp dirs for mavencodeanalysis tests to save into
113111
function createTempDirsForSonarQubeTests(): void {
112+
var sqTempDir: string = path.join(createTempDir(), '.sqAnalysis');
113+
114+
if (!fs.existsSync(sqTempDir)) {
115+
fs.mkdirSync(sqTempDir);
116+
}
117+
}
118+
119+
function createTempDir():string {
114120
var testTempDir: string = path.join(__dirname, '_temp');
115-
var sqTempDir: string = path.join(testTempDir, '.sqAnalysis');
116121

117122
if (!fs.existsSync(testTempDir)) {
118123
fs.mkdirSync(testTempDir);
119124
}
120125

121-
if (!fs.existsSync(sqTempDir)) {
122-
fs.mkdirSync(sqTempDir);
123-
}
126+
return testTempDir;
124127
}
125128

126129
function captureStream(stream):{unhook():void, captured():string} {
@@ -1968,4 +1971,226 @@ describe('Maven Suite', function () {
19681971
assert(taskResult == 0 /* TaskResult.Failed == 0 */, 'Task should not have failed.');
19691972
});
19701973
});
1974+
1975+
/* Standalone Code Analysis unit tests */
1976+
1977+
it('Code Analysis common - createDirectory correctly creates new dir', () => {
1978+
// Arrange
1979+
var testStgDir: string = path.join(__dirname, '_temp');
1980+
1981+
if (!fs.existsSync(testStgDir)) {
1982+
fs.mkdirSync(testStgDir);
1983+
}
1984+
1985+
var newFolder1 = path.join(testStgDir, 'fish');
1986+
1987+
// Act
1988+
FileSystemInteractions.createDirectory(newFolder1);
1989+
1990+
// Assert
1991+
assert(fs.existsSync(newFolder1), `Expected folder to have been created: ${newFolder1}`);
1992+
1993+
deleteFolderRecursive(testStgDir);
1994+
});
1995+
1996+
it('Code Analysis common - createDirectory correctly creates new dir and 1 directory in between', () => {
1997+
// Arrange
1998+
var testStgDir: string = path.join(__dirname, '_temp');
1999+
var newFolder1 = path.join(testStgDir, 'fish');
2000+
2001+
if (!fs.existsSync(testStgDir)) {
2002+
fs.mkdirSync(testStgDir);
2003+
}
2004+
if (!fs.existsSync(newFolder1)) {
2005+
fs.mkdirSync(newFolder1);
2006+
}
2007+
2008+
var newFolder2 = path.join(testStgDir, 'fish', 'and');
2009+
var newFolder3 = path.join(testStgDir, 'fish', 'and', 'chips');
2010+
2011+
// Act
2012+
FileSystemInteractions.createDirectory(newFolder3);
2013+
2014+
// Assert
2015+
assert(fs.existsSync(newFolder1), `Expected folder to have been created: ${newFolder2}`);
2016+
assert(fs.existsSync(newFolder1), `Expected folder to have been created: ${newFolder3}`);
2017+
2018+
deleteFolderRecursive(testStgDir);
2019+
});
2020+
2021+
it('Code Analysis common - createDirectory correctly creates new dir and 2 directories in between', () => {
2022+
// Arrange
2023+
var testStgDir: string = path.join(__dirname, '_temp');
2024+
var newFolder1 = path.join(testStgDir, 'fish');
2025+
2026+
if (!fs.existsSync(testStgDir)) {
2027+
fs.mkdirSync(testStgDir);
2028+
}
2029+
if (!fs.existsSync(newFolder1)) {
2030+
fs.mkdirSync(newFolder1);
2031+
}
2032+
2033+
var newFolder2 = path.join(testStgDir, 'fish', 'and');
2034+
var newFolder3 = path.join(testStgDir, 'fish', 'and', 'chips');
2035+
2036+
// Act
2037+
FileSystemInteractions.createDirectory(newFolder3);
2038+
2039+
// Assert
2040+
assert(fs.existsSync(newFolder1), `Expected folder to have been created: ${newFolder2}`);
2041+
assert(fs.existsSync(newFolder1), `Expected folder to have been created: ${newFolder3}`);
2042+
2043+
deleteFolderRecursive(testStgDir);
2044+
});
2045+
2046+
it('Code Analysis common - createDirectory correctly creates new dir and all directories in between', () => {
2047+
// Arrange
2048+
var testStgDir: string = path.join(__dirname, '_temp');
2049+
2050+
if (!fs.existsSync(testStgDir)) {
2051+
fs.mkdirSync(testStgDir);
2052+
}
2053+
2054+
var newFolder1 = path.join(testStgDir, 'fish');
2055+
var newFolder2 = path.join(testStgDir, 'fish', 'and');
2056+
var newFolder3 = path.join(testStgDir, 'fish', 'and', 'chips');
2057+
2058+
// Act
2059+
FileSystemInteractions.createDirectory(newFolder3);
2060+
2061+
// Assert
2062+
assert(fs.existsSync(newFolder1), `Expected folder to have been created: ${newFolder1}`);
2063+
assert(fs.existsSync(newFolder1), `Expected folder to have been created: ${newFolder2}`);
2064+
assert(fs.existsSync(newFolder1), `Expected folder to have been created: ${newFolder3}`);
2065+
2066+
deleteFolderRecursive(testStgDir);
2067+
});
2068+
2069+
it('Code Analysis common - createDirectory correctly creates new dir and all directories in between (repeating dir names)', () => {
2070+
// Arrange
2071+
var testStgDir: string = path.join(__dirname, '_temp');
2072+
2073+
if (!fs.existsSync(testStgDir)) {
2074+
fs.mkdirSync(testStgDir);
2075+
}
2076+
2077+
var newFolder1 = path.join(testStgDir, 'fish');
2078+
var newFolder2 = path.join(testStgDir, 'fish', 'and');
2079+
var newFolder3 = path.join(testStgDir, 'fish', 'and', 'fish');
2080+
2081+
// Act
2082+
FileSystemInteractions.createDirectory(newFolder3);
2083+
2084+
// Assert
2085+
assert(fs.existsSync(newFolder1), `Expected folder to have been created: ${newFolder1}`);
2086+
assert(fs.existsSync(newFolder1), `Expected folder to have been created: ${newFolder2}`);
2087+
assert(fs.existsSync(newFolder1), `Expected folder to have been created: ${newFolder3}`);
2088+
2089+
deleteFolderRecursive(testStgDir);
2090+
});
2091+
2092+
// Copied from: https://github.com/Microsoft/vsts-task-lib/blob/master/node/test/dirtests.ts
2093+
it('Code Analysis common - createDirectory fails with illegal chars', function (done) {
2094+
this.timeout(1000);
2095+
2096+
var testPath = path.join(createTempDir(), 'mkdir\0');
2097+
var worked: boolean = false;
2098+
try {
2099+
FileSystemInteractions.createDirectory(testPath);
2100+
worked = true;
2101+
}
2102+
catch (err) {
2103+
// asserting failure
2104+
assert(!shell.test('-d', testPath), 'directory should not be created');
2105+
}
2106+
2107+
assert(!worked, 'mkdirP with illegal chars should have not have worked');
2108+
2109+
done();
2110+
});
2111+
2112+
// Copied from: https://github.com/Microsoft/vsts-task-lib/blob/master/node/test/dirtests.ts
2113+
it('Code Analysis common - createDirectory fails with null path', function (done) {
2114+
this.timeout(1000);
2115+
2116+
var worked: boolean = false;
2117+
try {
2118+
FileSystemInteractions.createDirectory(null);
2119+
worked = true;
2120+
}
2121+
catch (err) { }
2122+
2123+
assert(!worked, 'mkdirP with null should have not have worked');
2124+
2125+
done();
2126+
});
2127+
2128+
// Copied from: https://github.com/Microsoft/vsts-task-lib/blob/master/node/test/dirtests.ts
2129+
it('Code Analysis common - createDirectory fails with empty path', function (done) {
2130+
this.timeout(1000);
2131+
2132+
var worked: boolean = false;
2133+
try {
2134+
FileSystemInteractions.createDirectory('');
2135+
worked = true;
2136+
}
2137+
catch (err) { }
2138+
2139+
assert(!worked, 'mkdirP with empty string should have not have worked');
2140+
2141+
done();
2142+
});
2143+
2144+
// Copied from: https://github.com/Microsoft/vsts-task-lib/blob/master/node/test/dirtests.ts
2145+
it('Code Analysis common - createDirectory fails with conflicting file path', (done: MochaDone) => {
2146+
this.timeout(1000);
2147+
2148+
let testPath = path.join(createTempDir(), 'mkdirP_conflicting_file_path');
2149+
shell.mkdir('-p', createTempDir());
2150+
fs.writeFileSync(testPath, '');
2151+
let worked: boolean = false;
2152+
try {
2153+
FileSystemInteractions.createDirectory(testPath);
2154+
worked = true;
2155+
}
2156+
catch (err) { }
2157+
2158+
assert(!worked, 'mkdirP with conflicting file path should not have worked');
2159+
2160+
done();
2161+
});
2162+
2163+
// Copied from: https://github.com/Microsoft/vsts-task-lib/blob/master/node/test/dirtests.ts
2164+
it('Code Analysis common - createDirectory fails with conflicting parent file path', (done: MochaDone) => {
2165+
this.timeout(1000);
2166+
2167+
let testPath = path.join(createTempDir(), 'mkdirP_conflicting_parent_file_path', 'dir');
2168+
shell.mkdir('-p', createTempDir());
2169+
fs.writeFileSync(path.dirname(testPath), '');
2170+
let worked: boolean = false;
2171+
try {
2172+
FileSystemInteractions.createDirectory(testPath);
2173+
worked = true;
2174+
}
2175+
catch (err) { }
2176+
2177+
assert(!worked, 'mkdirP with conflicting file path should not have worked');
2178+
2179+
done();
2180+
});
2181+
2182+
// Copied from: https://github.com/Microsoft/vsts-task-lib/blob/master/node/test/dirtests.ts
2183+
it('Code Analysis common - createDirectory no-ops if mkdirP directory exists', (done: MochaDone) => {
2184+
this.timeout(1000);
2185+
2186+
let testPath = path.join(createTempDir(), 'mkdirP_dir_exists');
2187+
shell.mkdir('-p', createTempDir());
2188+
fs.mkdirSync(testPath);
2189+
2190+
FileSystemInteractions.createDirectory(testPath); // should not throw
2191+
2192+
done();
2193+
});
2194+
2195+
19712196
});

0 commit comments

Comments
 (0)