Skip to content

Commit 0a21874

Browse files
authored
Merge pull request #1554 from arturcic/fix/use-absolute-target-path
Normalizes target path in dotnet tool
2 parents 3daa9ce + 2ba6229 commit 0a21874

File tree

8 files changed

+84
-135
lines changed

8 files changed

+84
-135
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ jobs:
6060
- name: gitversion/command (showvariable)
6161
uses: ./gitversion/command
6262
with:
63+
targetPath: './'
6364
arguments: '/showvariable FullSemVer'
6465
- name: gitversion/command (format)
6566
uses: ./gitversion/command
6667
with:
68+
targetPath: './'
6769
arguments: '/format {Major}.{Minor}'
6870
- name: gitversion/execute
6971
id: gitversion # step id used as reference for output values

dist/tools/libs/tools.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,16 @@ class DotnetTool {
307307
if (!targetPath) {
308308
workDir = srcDir;
309309
} else {
310+
if (!path.isAbsolute(targetPath)) {
311+
targetPath = path.resolve(targetPath);
312+
}
310313
if (await this.buildAgent.directoryExists(targetPath)) {
311314
workDir = targetPath;
312315
} else {
313316
throw new Error(`Directory not found at ${targetPath}`);
314317
}
315318
}
316-
return workDir.replace(/\\/g, "/");
319+
return path.normalize(workDir);
317320
}
318321
async queryLatestMatch(toolName, versionSpec, includePrerelease) {
319322
this.buildAgent.info(

dist/tools/libs/tools.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/tools/common/dotnet-tool.spec.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
22
import * as os from 'node:os'
33
import * as path from 'node:path'
44
import * as fs from 'node:fs/promises'
5+
import * as process from 'node:process'
56
import { DotnetTool, ISettingsProvider } from '@tools/common'
67
import { IBuildAgent } from '@agents/common'
78
import { Dirent } from 'node:fs'
@@ -24,6 +25,10 @@ class TestDotnetTool extends DotnetTool {
2425
return 'TEST_TOOL_PATH'
2526
}
2627

28+
async getRepoPath(targetPath: string): Promise<string> {
29+
return super.getRepoPath(targetPath)
30+
}
31+
2732
get versionRange(): string | null {
2833
return null
2934
}
@@ -242,4 +247,61 @@ describe('DotnetTool', () => {
242247
expect(mockSetVariable).toHaveBeenCalledWith('DOTNET_NOLOGO', 'true')
243248
})
244249
})
250+
251+
describe('getRepoDir', () => {
252+
it('should return correct repo dir for empty target path, takes build agent sourceDir', async () => {
253+
const buildAgent = {
254+
sourceDir: '/workdir'
255+
} as IBuildAgent
256+
tool = new TestDotnetTool(buildAgent)
257+
const repoDir = await tool.getRepoPath('')
258+
expect(repoDir).toBe(path.normalize('/workdir'))
259+
})
260+
261+
it('should return correct repo dir for empty target path, takes default', async () => {
262+
const buildAgent = {
263+
sourceDir: ''
264+
} as IBuildAgent
265+
tool = new TestDotnetTool(buildAgent)
266+
const repoDir = await tool.getRepoPath('')
267+
expect(repoDir).toBe('.')
268+
})
269+
270+
it('should return correct repo dir for existing absolute target path', async () => {
271+
const buildAgent = {
272+
async directoryExists(_file: string): Promise<boolean> {
273+
return Promise.resolve(true)
274+
}
275+
} as IBuildAgent
276+
tool = new TestDotnetTool(buildAgent)
277+
const repoDir = await tool.getRepoPath('/targetDir')
278+
expect(repoDir).toBe(path.normalize('/targetDir'))
279+
})
280+
281+
it('should return correct repo dir for existing relative target path', async () => {
282+
const targetPath = 'targetDir'
283+
const buildAgent = {
284+
async directoryExists(_file: string): Promise<boolean> {
285+
return Promise.resolve(true)
286+
}
287+
} as IBuildAgent
288+
tool = new TestDotnetTool(buildAgent)
289+
const repoDir = await tool.getRepoPath(targetPath)
290+
const resolvedPath = path.join(process.cwd(), 'targetDir')
291+
expect(repoDir).toBe(resolvedPath)
292+
})
293+
294+
it('should throw error for non-existing target path', async () => {
295+
const wrongDir = 'wrongdir'
296+
const buildAgent = {
297+
async directoryExists(_file: string): Promise<boolean> {
298+
return Promise.resolve(false)
299+
}
300+
} as IBuildAgent
301+
tool = new TestDotnetTool(buildAgent)
302+
303+
const resolvedPath = path.join(process.cwd(), wrongDir)
304+
await expect(tool.getRepoPath(wrongDir)).rejects.toThrowError(`Directory not found at ${resolvedPath}`)
305+
})
306+
})
245307
})

src/__tests__/tools/gitreleasemanager/tool.spec.ts

Lines changed: 10 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -79,64 +79,12 @@ describe('GitReleaseManagerTool', () => {
7979
expect(tool.settingsProvider).toBeDefined()
8080
})
8181

82-
describe('getRepoDir', () => {
83-
it('should return correct repo dir for empty target path, takes build agent sourceDir', async () => {
84-
const buildAgent = {
85-
sourceDir: 'workdir'
86-
} as IBuildAgent
87-
tool = new TestGitReleaseManagerTool(buildAgent)
88-
const repoDir = await tool.getRepoDir({
89-
targetDirectory: ''
90-
} as CommonSettings)
91-
expect(repoDir).toBe('workdir')
92-
})
93-
94-
it('should return correct repo dir for empty target path, takes default', async () => {
95-
const buildAgent = {
96-
sourceDir: ''
97-
} as IBuildAgent
98-
tool = new TestGitReleaseManagerTool(buildAgent)
99-
const repoDir = await tool.getRepoDir({
100-
targetDirectory: ''
101-
} as CommonSettings)
102-
expect(repoDir).toBe('.')
103-
})
104-
105-
it('should return correct repo dir for existing target path', async () => {
106-
const buildAgent = {
107-
async directoryExists(_file: string): Promise<boolean> {
108-
return Promise.resolve(true)
109-
}
110-
} as IBuildAgent
111-
tool = new TestGitReleaseManagerTool(buildAgent)
112-
const repoDir = await tool.getRepoDir({
113-
targetDirectory: 'targetDir'
114-
} as CommonSettings)
115-
expect(repoDir).toBe('targetDir')
116-
})
117-
118-
it('should throw error for non-existing target path', async () => {
119-
const wrongDir = 'wrongdir'
120-
const buildAgent = {
121-
async directoryExists(_file: string): Promise<boolean> {
122-
return Promise.resolve(false)
123-
}
124-
} as IBuildAgent
125-
tool = new TestGitReleaseManagerTool(buildAgent)
126-
await expect(
127-
tool.getRepoDir({
128-
targetDirectory: wrongDir
129-
} as CommonSettings)
130-
).rejects.toThrowError(`Directory not found at ${wrongDir}`)
131-
})
132-
})
133-
13482
describe('getArguments', () => {
13583
const commonSettings = {
13684
owner: 'owner',
13785
repository: 'repo',
13886
token: 'token',
139-
targetDirectory: 'targetDirectory',
87+
targetDirectory: '/targetDirectory',
14088
logFilePath: './logFilePath'
14189
} as CommonSettings
14290

@@ -171,28 +119,11 @@ describe('GitReleaseManagerTool', () => {
171119
'--token',
172120
'token',
173121
'--targetDirectory',
174-
'targetDirectory',
122+
path.normalize('/targetDirectory'),
175123
'--logFilePath',
176124
'./logFilePath'
177125
])
178126
})
179-
180-
it('should throw error for non-existing target path', async () => {
181-
tool = new TestGitReleaseManagerTool({
182-
async directoryExists(_file: string): Promise<boolean> {
183-
return Promise.resolve(false)
184-
}
185-
} as IBuildAgent)
186-
187-
const wrongDir = 'wrongdir'
188-
189-
await expect(
190-
tool.getCommonArguments({
191-
...commonSettings,
192-
targetDirectory: wrongDir
193-
} as CommonSettings)
194-
).rejects.toThrowError(`Directory not found at ${wrongDir}`)
195-
})
196127
})
197128

198129
describe('getCreateArguments', () => {
@@ -217,7 +148,7 @@ describe('GitReleaseManagerTool', () => {
217148
'--token',
218149
'token',
219150
'--targetDirectory',
220-
'targetDirectory',
151+
path.normalize('/targetDirectory'),
221152
'--logFilePath',
222153
'./createLogFilePath',
223154
'--milestone',
@@ -230,7 +161,7 @@ describe('GitReleaseManagerTool', () => {
230161
'inputFilePath',
231162
'--pre',
232163
'--assets',
233-
`${path.join('targetDirectory', 'asset1')},${path.join('targetDirectory', 'asset2')}`
164+
`${path.join('/targetDirectory', 'asset1')},${path.join('/targetDirectory', 'asset2')}`
234165
])
235166
})
236167
})
@@ -251,7 +182,7 @@ describe('GitReleaseManagerTool', () => {
251182
'--token',
252183
'token',
253184
'--targetDirectory',
254-
'targetDirectory',
185+
path.normalize('/targetDirectory'),
255186
'--logFilePath',
256187
'./logFilePath',
257188
'--milestone',
@@ -276,7 +207,7 @@ describe('GitReleaseManagerTool', () => {
276207
'--token',
277208
'token',
278209
'--targetDirectory',
279-
'targetDirectory',
210+
path.normalize('/targetDirectory'),
280211
'--logFilePath',
281212
'./logFilePath',
282213
'--milestone',
@@ -301,7 +232,7 @@ describe('GitReleaseManagerTool', () => {
301232
'--token',
302233
'token',
303234
'--targetDirectory',
304-
'targetDirectory',
235+
path.normalize('/targetDirectory'),
305236
'--logFilePath',
306237
'./logFilePath',
307238
'--milestone',
@@ -326,7 +257,7 @@ describe('GitReleaseManagerTool', () => {
326257
'--token',
327258
'token',
328259
'--targetDirectory',
329-
'targetDirectory',
260+
path.normalize('/targetDirectory'),
330261
'--logFilePath',
331262
'./logFilePath',
332263
'--tagName',
@@ -352,13 +283,13 @@ describe('GitReleaseManagerTool', () => {
352283
'--token',
353284
'token',
354285
'--targetDirectory',
355-
'targetDirectory',
286+
path.normalize('/targetDirectory'),
356287
'--logFilePath',
357288
'./logFilePath',
358289
'--tagName',
359290
'milestone',
360291
'--assets',
361-
`${path.join('targetDirectory', 'asset1')},${path.join('targetDirectory', 'asset2')}`
292+
`${path.join('/targetDirectory', 'asset1')},${path.join('/targetDirectory', 'asset2')}`
362293
])
363294
})
364295
})

src/__tests__/tools/gitversion/runner.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('GitVersion Runner', () => {
8484
expect(getEnv('patch')).toBeDefined()
8585
})
8686

87-
it.sequential('should output Major variable', async () => {
87+
it.sequential('should output Sha variable', async () => {
8888
setEnv(toolPathVariable, toolPath)
8989

9090
setInputs({

src/__tests__/tools/gitversion/tool.spec.ts

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -112,58 +112,6 @@ describe('GitVersionTool', () => {
112112
})
113113
})
114114

115-
describe('getRepoDir', () => {
116-
it('should return correct repo dir for empty target path, takes build agent sourceDir', async () => {
117-
const buildAgent = {
118-
sourceDir: 'workdir'
119-
} as IBuildAgent
120-
tool = new TestGitVersionTool(buildAgent)
121-
const repoDir = await tool.getRepoDir({
122-
targetPath: ''
123-
} as ExecuteSettings | CommandSettings)
124-
expect(repoDir).toBe('workdir')
125-
})
126-
127-
it('should return correct repo dir for empty target path, takes default', async () => {
128-
const buildAgent = {
129-
sourceDir: ''
130-
} as IBuildAgent
131-
tool = new TestGitVersionTool(buildAgent)
132-
const repoDir = await tool.getRepoDir({
133-
targetPath: ''
134-
} as ExecuteSettings | CommandSettings)
135-
expect(repoDir).toBe('.')
136-
})
137-
138-
it('should return correct repo dir for existing target path', async () => {
139-
const buildAgent = {
140-
async directoryExists(_file: string): Promise<boolean> {
141-
return Promise.resolve(true)
142-
}
143-
} as IBuildAgent
144-
tool = new TestGitVersionTool(buildAgent)
145-
const repoDir = await tool.getRepoDir({
146-
targetPath: 'targetDir'
147-
} as ExecuteSettings | CommandSettings)
148-
expect(repoDir).toBe('targetDir')
149-
})
150-
151-
it('should throw error for non-existing target path', async () => {
152-
const wrongDir = 'wrongdir'
153-
const buildAgent = {
154-
async directoryExists(_file: string): Promise<boolean> {
155-
return Promise.resolve(false)
156-
}
157-
} as IBuildAgent
158-
tool = new TestGitVersionTool(buildAgent)
159-
await expect(
160-
tool.getRepoDir({
161-
targetPath: wrongDir
162-
} as ExecuteSettings | CommandSettings)
163-
).rejects.toThrowError(`Directory not found at ${wrongDir}`)
164-
})
165-
})
166-
167115
describe('getExecuteArguments', () => {
168116
it('should return correct arguments for empty settings', async () => {
169117
const args = await tool.getExecuteArguments('workdir', {} as ExecuteSettings)

src/tools/common/dotnet-tool.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,16 @@ export abstract class DotnetTool implements IDotnetTool {
216216
if (!targetPath) {
217217
workDir = srcDir
218218
} else {
219+
if (!path.isAbsolute(targetPath)) {
220+
targetPath = path.resolve(targetPath)
221+
}
219222
if (await this.buildAgent.directoryExists(targetPath)) {
220223
workDir = targetPath
221224
} else {
222225
throw new Error(`Directory not found at ${targetPath}`)
223226
}
224227
}
225-
return workDir.replace(/\\/g, '/')
228+
return path.normalize(workDir)
226229
}
227230

228231
private async queryLatestMatch(toolName: string, versionSpec: string, includePrerelease: boolean): Promise<string | null> {

0 commit comments

Comments
 (0)