Skip to content

Commit f3399b1

Browse files
Copilotsanjuyadav24
andcommitted
Fix DotNetCoreCLI@2 zipAfterPublish issue to prevent empty directories
Co-authored-by: sanjuyadav24 <[email protected]>
1 parent 7b5820b commit f3399b1

File tree

3 files changed

+108
-6
lines changed

3 files changed

+108
-6
lines changed

Tasks/DotNetCoreCLIV2/Tests/L0.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,16 @@ describe('DotNetCoreExe Suite', function () {
321321
assert(tr.succeeded, 'task should have succeeded');
322322
});
323323

324-
it('publish works with zipAfterPublish option', () => {
325-
// TODO
324+
it('publish works with zipAfterPublish option', async () => {
325+
process.env["__projects__"] = "web/project.json";
326+
process.env["__publishWebProjects__"] = "false";
327+
process.env["__arguments__"] = "--configuration release --output /usr/out";
328+
let tp = path.join(__dirname, 'zipAfterPublishTests.js');
329+
let tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);
330+
await tr.runAsync();
331+
332+
assert(tr.invokedToolCount == 1, 'should have invoked tool once');
333+
assert(tr.succeeded, 'task should have succeeded');
326334
});
327335

328336
it('publish fails with zipAfterPublish and publishWebProjects option with no project file specified', async () => {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import ma = require('azure-pipelines-task-lib/mock-answer');
2+
import tmrm = require('azure-pipelines-task-lib/mock-run');
3+
import path = require('path');
4+
import fs = require('fs');
5+
import assert = require('assert');
6+
7+
let taskPath = path.join(__dirname, '..', 'dotnetcore.js');
8+
let tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);
9+
10+
tmr.setInput('command', "publish");
11+
tmr.setInput('projects', "web/project.json");
12+
tmr.setInput('publishWebProjects', "false");
13+
tmr.setInput('arguments', "--configuration release --output /usr/out");
14+
tmr.setInput('zipAfterPublish', "true");
15+
tmr.setInput('modifyOutputPath', "false");
16+
17+
// Mock file system operations for testing zip functionality
18+
const mockFs = {
19+
createWriteStream: function(filePath) {
20+
console.log("Creating write stream for: " + filePath);
21+
const events = {};
22+
return {
23+
on: (event, callback) => {
24+
events[event] = callback;
25+
return this;
26+
},
27+
end: () => {
28+
console.log("Closing write stream for: " + filePath);
29+
events['close']();
30+
}
31+
};
32+
},
33+
mkdirSync: function(p) {
34+
console.log("Creating directory: " + p);
35+
},
36+
renameSync: function(oldPath, newPath) {
37+
console.log("Moving file from: " + oldPath + " to: " + newPath);
38+
},
39+
existsSync: function(filePath) {
40+
return true;
41+
},
42+
readFileSync: function() {
43+
return "";
44+
},
45+
statSync: function() {
46+
return {
47+
isFile: () => false,
48+
isDirectory: () => true
49+
};
50+
},
51+
lstatSync: function() {
52+
return {
53+
isDirectory: () => true
54+
};
55+
}
56+
};
57+
58+
// Mock archiver
59+
const mockArchiver = function() {
60+
return {
61+
pipe: function() { return this; },
62+
directory: function() { return this; },
63+
finalize: function() { return this; }
64+
};
65+
};
66+
67+
let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
68+
"which": { "dotnet": "dotnet" },
69+
"checkPath": { "dotnet": true },
70+
"exist": {
71+
"/usr/out": true
72+
},
73+
"exec": {
74+
"dotnet publish web/project.json --configuration release --output /usr/out": {
75+
"code": 0,
76+
"stdout": "published web without adding project name to path\n",
77+
"stderr": ""
78+
}
79+
},
80+
"findMatch": {
81+
"web/project.json": ["web/project.json"]
82+
},
83+
"rmRF": {
84+
"/usr/out": {
85+
"success": true
86+
}
87+
}
88+
};
89+
90+
tmr.setAnswers(a);
91+
tmr.registerMock('fs', Object.assign({}, fs, mockFs));
92+
tmr.registerMock('archiver', mockArchiver);
93+
tmr.registerMock('azure-pipelines-task-lib/toolrunner', require('azure-pipelines-task-lib/mock-toolrunner'));
94+
95+
tmr.run();

Tasks/DotNetCoreCLIV2/dotnetcore.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,9 @@ export class dotNetExe {
286286
var outputTarget = outputSource + ".zip";
287287
await this.zip(outputSource, outputTarget);
288288
tl.rmRF(outputSource);
289-
if (moveZipToOutputSource) {
290-
fs.mkdirSync(outputSource);
291-
fs.renameSync(outputTarget, path.join(outputSource, path.basename(outputTarget)));
292-
}
289+
// When moveZipToOutputSource is true and zipAfterPublish is true,
290+
// we should leave the zip file where it was created and not move it into a subdirectory
291+
// This way the artifact will be the zip file directly instead of a directory containing the zip
293292
}
294293
else {
295294
throw tl.loc("noPublishFolderFoundToZip", projectFile);

0 commit comments

Comments
 (0)