Skip to content

Commit de21bd1

Browse files
add unit tests and take into account root path
1 parent 53f52c0 commit de21bd1

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/ci/CIInfo.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public CIInfo(
170170
this.ciPipelineNumber = ciPipelineNumber;
171171
this.ciPipelineUrl = ciPipelineUrl;
172172
this.ciJobUrl = ciJobUrl;
173-
this.ciWorkspace = ciWorkspace;
173+
this.ciWorkspace = sanitizeWorkspace(ciWorkspace);
174174
this.ciNodeName = ciNodeName;
175175
this.ciNodeLabels = ciNodeLabels;
176176
this.ciEnvVars = ciEnvVars;
@@ -209,14 +209,20 @@ public String getCiJobUrl() {
209209
return ciJobUrl;
210210
}
211211

212-
/** @return Workspace path without the trailing separator */
213-
public String getCiWorkspace() {
214-
String realCiWorkspace = FileUtils.toRealPath(ciWorkspace);
215-
return (realCiWorkspace == null || !realCiWorkspace.endsWith(File.separator))
212+
private String sanitizeWorkspace(String workspace) {
213+
String realCiWorkspace = FileUtils.toRealPath(workspace);
214+
return (realCiWorkspace == null
215+
|| !realCiWorkspace.endsWith(File.separator)
216+
|| realCiWorkspace.length() == 1) // root path "/"
216217
? realCiWorkspace
217218
: (realCiWorkspace.substring(0, realCiWorkspace.length() - 1));
218219
}
219220

221+
/** @return Workspace path without the trailing separator */
222+
public String getCiWorkspace() {
223+
return ciWorkspace;
224+
}
225+
220226
public String getCiNodeName() {
221227
return ciNodeName;
222228
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package datadog.trace.civisibility.ci
2+
3+
4+
import spock.lang.Specification
5+
6+
class CIInfoTest extends Specification {
7+
8+
def "test ci workspace is correctly sanitized #iterationIndex"() {
9+
def builder = CIInfo.builder(null)
10+
builder.ciWorkspace(workspacePath)
11+
def info = builder.build()
12+
13+
info.ciWorkspace == sanitizedPath
14+
15+
where:
16+
workspacePath | sanitizedPath
17+
null | null
18+
"/" | "/"
19+
"/repo/path" | "/repo/path"
20+
"/repo/path/" | "/repo/path"
21+
}
22+
}

internal-api/src/main/java/datadog/trace/api/git/GitInfoProvider.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public GitInfo getGitInfo(@Nullable String repositoryPath) {
5656
repositoryPath = NULL_PATH_STRING;
5757
}
5858

59-
// normalize path to avoid creating two entries in the cache
6059
return gitInfoCache.computeIfAbsent(repositoryPath, this::buildGitInfo);
6160
}
6261

0 commit comments

Comments
 (0)