Skip to content

Commit 5ddb07b

Browse files
Implement file-granularity diff
1 parent 7d58819 commit 5ddb07b

File tree

5 files changed

+126
-2
lines changed

5 files changed

+126
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public interface Diff extends SerializableType {
77

88
Diff EMPTY = LineDiff.EMPTY;
99

10-
PolymorphicSerializer<Diff> SERIALIZER = new PolymorphicSerializer<>(LineDiff.class);
10+
PolymorphicSerializer<Diff> SERIALIZER =
11+
new PolymorphicSerializer<>(LineDiff.class, FileDiff.class);
1112

1213
boolean contains(String relativePath, int startLine, int endLine);
1314
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package datadog.trace.civisibility.diff;
2+
3+
import datadog.trace.civisibility.ipc.serialization.Serializer;
4+
import java.nio.ByteBuffer;
5+
import java.util.Objects;
6+
import java.util.Set;
7+
import javax.annotation.Nonnull;
8+
9+
/** Diff data with per-file granularity. */
10+
public class FileDiff implements Diff {
11+
12+
private final @Nonnull Set<String> changedFiles;
13+
14+
public FileDiff(@Nonnull Set<String> changedFiles) {
15+
this.changedFiles = changedFiles;
16+
}
17+
18+
@Override
19+
public boolean contains(String relativePath, int startLine, int endLine) {
20+
return changedFiles.contains(relativePath);
21+
}
22+
23+
@Override
24+
public void serialize(Serializer s) {
25+
s.write(changedFiles);
26+
}
27+
28+
public static FileDiff deserialize(ByteBuffer buffer) {
29+
return new FileDiff(Serializer.readSet(buffer, Serializer::readString));
30+
}
31+
32+
@Override
33+
public boolean equals(Object o) {
34+
if (this == o) {
35+
return true;
36+
}
37+
if (o == null || getClass() != o.getClass()) {
38+
return false;
39+
}
40+
FileDiff diff = (FileDiff) o;
41+
return Objects.equals(changedFiles, diff.changedFiles);
42+
}
43+
44+
@Override
45+
public int hashCode() {
46+
return Objects.hashCode(changedFiles);
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return "FileDiff{changedFiles=" + changedFiles + '}';
52+
}
53+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package datadog.trace.civisibility.diff
2+
3+
import datadog.trace.civisibility.ipc.serialization.Serializer
4+
import spock.lang.Specification
5+
6+
import static datadog.trace.civisibility.TestUtils.lines
7+
8+
class DiffTest extends Specification {
9+
10+
def "test diffs serialization: #diff"() {
11+
when:
12+
def s = new Serializer()
13+
Diff.SERIALIZER.serialize(diff, s)
14+
def buffer = s.flush()
15+
def diffCopy = Diff.SERIALIZER.deserialize(buffer)
16+
17+
then:
18+
diff == diffCopy
19+
20+
where:
21+
diff << [
22+
new FileDiff(Collections.emptySet()),
23+
new FileDiff(Collections.singleton("path")),
24+
new FileDiff(new HashSet<>(["path-a", "path-b"])),
25+
new LineDiff([:]),
26+
new LineDiff(["path": lines(10)]),
27+
new LineDiff(["path": lines(10, 11, 13)]),
28+
new LineDiff(["path": lines(10, 11, 12, 13), "path-b": lines()]),
29+
new LineDiff(["path": lines(10, 11, 12, 13), "path-b": lines(8, 18)])
30+
]
31+
}
32+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package datadog.trace.civisibility.diff
2+
3+
import datadog.trace.civisibility.ipc.serialization.Serializer
4+
import spock.lang.Specification
5+
6+
class FileDiffTest extends Specification {
7+
8+
def "test diff contains file"() {
9+
when:
10+
def diff = new FileDiff(new HashSet<>(paths))
11+
12+
then:
13+
diff.contains(path, 0, Integer.MAX_VALUE) == result
14+
15+
where:
16+
paths | path | result
17+
["path-a"] | "path-a" | true
18+
["path-a"] | "path-b" | false
19+
["path-a", "path-b"] | "path-a" | true
20+
["path-a", "path-b"] | "path-b" | true
21+
["path-a", "path-b"] | "path-c" | false
22+
}
23+
24+
def "test serialization: #paths"() {
25+
given:
26+
def diff = new FileDiff(new HashSet<>(paths))
27+
28+
when:
29+
def serializer = new Serializer()
30+
diff.serialize(serializer)
31+
def buf = serializer.flush()
32+
33+
then:
34+
FileDiff.deserialize(buf) == diff
35+
36+
where:
37+
paths << [[], ["path-a"], ["path-a", "path-b"]]
38+
}
39+
}

dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/diff/LineDiffTest.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class LineDiffTest extends Specification {
3939

4040
def "test serialization: #lines"() {
4141
given:
42-
4342
def diff = new LineDiff(lines)
4443

4544
when:

0 commit comments

Comments
 (0)