Skip to content

Commit

Permalink
Move showDiffDetails property to ComparisonData
Browse files Browse the repository at this point in the history
This simplifies testing of comparators with different values of this
property.
  • Loading branch information
HannesWell authored and laeubi committed Jul 7, 2023
1 parent 1daa6ea commit 5b5d2b1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@

public interface ArtifactComparator {

public static class ComparisonData {
public static record ComparisonData(List<String> ignoredPattern, boolean writeDelta, boolean showDiffDetails) {

public ComparisonData(List<String> ignoredPattern, boolean writeDelta) {
this.ignoredPattern = ignoredPattern != null ? List.copyOf(ignoredPattern) : List.of();
this.writeDelta = writeDelta;
}
/**
* System property that control if a detailed diff is desired or not, <code>false</code>
* (default) = no detailed diff is shown, <code>true</code> show detailed difference.
*/
private static final boolean SHOW_DIFF_DETAILS = Boolean.getBoolean("tycho.comparator.showDiff");

private final List<String> ignoredPattern;
private boolean writeDelta;

public List<String> ignoredPattern() {
return ignoredPattern;
public ComparisonData(List<String> ignoredPattern, boolean writeDelta) {
this(ignoredPattern, writeDelta, SHOW_DIFF_DETAILS);
}

public boolean writeDelta() {
return writeDelta;
public ComparisonData(List<String> ignoredPattern, boolean writeDelta, boolean showDiffDetails) {
this.ignoredPattern = ignoredPattern != null ? List.copyOf(ignoredPattern) : List.of();
this.writeDelta = writeDelta;
this.showDiffDetails = showDiffDetails;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@

public interface ContentsComparator {

/**
* System property that control if a detailed diff is desired or not, <code>false</code>
* (default) = no detailed diff is shown, <code>true</code> show detailed difference.
*/
static final boolean SHOW_DIFF_DETAILS = Boolean.getBoolean("tycho.comparator.showDiff");

/**
* System property that controls the threshold size where a direct byte compare is performed
* (default 5 mb)
Expand All @@ -36,7 +30,7 @@ public interface ContentsComparator {
/**
* Computes the delta for the given {@link InputStream}s, the streams passed will support
* mark/reset for repeated reads.
*
*
* @param baseline
* the baseline data
* @param reactor
Expand All @@ -51,7 +45,7 @@ public ArtifactDelta getDelta(ComparatorInputStream baseline, ComparatorInputStr

/**
* Check if this comparator matches the given name or extension
*
*
* @param nameOrExtension
* the extension or name to match
* @return <code>true</code> if this comparator matches, <code>false</code> otherwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ArtifactDelta getDelta(ComparatorInputStream baseline, ComparatorInputStr
throws IOException {
if (isTextFile(baseline) && isTextFile(reactor)) {
//If both items a certainly a text file, we compare them ignoring line endings
return TextComparator.compareText(baseline, reactor);
return TextComparator.compareText(baseline, reactor, data);
}
return ArtifactDelta.DEFAULT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
Expand Down Expand Up @@ -39,21 +39,21 @@ public class TextComparator implements ContentsComparator {
@Override
public ArtifactDelta getDelta(ComparatorInputStream baseline, ComparatorInputStream reactor, ComparisonData data)
throws IOException {
return compareText(baseline, reactor);
return compareText(baseline, reactor, data);
}

public static ArtifactDelta compareText(ComparatorInputStream baseline, ComparatorInputStream reactor)
throws IOException {
public static ArtifactDelta compareText(ComparatorInputStream baseline, ComparatorInputStream reactor,
ComparisonData data) throws IOException {
ByteIterator baselineIterator = new ByteIterator(baseline.asBytes());
ByteIterator reactorIterator = new ByteIterator(reactor.asBytes());
while (baselineIterator.hasNext() && reactorIterator.hasNext()) {
if (baselineIterator.next() != reactorIterator.next()) {
return createDelta(ArtifactDelta.DEFAULT.getMessage(), baseline, reactor);
return createDelta(ArtifactDelta.DEFAULT.getMessage(), baseline, reactor, data);
}
}
//now both need to be at the end of the stream if they are the same!
if (baselineIterator.hasNext() || reactorIterator.hasNext()) {
return createDelta(ArtifactDelta.DEFAULT.getMessage(), baseline, reactor);
return createDelta(ArtifactDelta.DEFAULT.getMessage(), baseline, reactor, data);
}
return ArtifactDelta.NO_DIFFERENCE;
}
Expand Down Expand Up @@ -100,8 +100,8 @@ public boolean matches(String nameOrExtension) {
}

public static ArtifactDelta createDelta(String message, ComparatorInputStream baseline,
ComparatorInputStream reactor) {
if (SHOW_DIFF_DETAILS) {
ComparatorInputStream reactor, ComparisonData data) {
if (data.showDiffDetails()) {
String detailed;
try {
List<String> source = IOUtils.readLines(baseline.asNewStream(), StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
Expand Down Expand Up @@ -43,21 +43,20 @@ public ArtifactDelta getDelta(ComparatorInputStream baseline, ComparatorInputStr
Diff baselineDiff = computeDiff(baseline, reactor);
if (baselineDiff.hasDifferences()) {
String message = baselineDiff.fullDescription();
return TextComparator.createDelta(message, baseline, reactor);
return TextComparator.createDelta(message, baseline, reactor, data);
}
return null;
} catch (RuntimeException e) {
return TextComparator.createDelta(ArtifactDelta.DEFAULT.getMessage(), baseline, reactor);
return TextComparator.createDelta(ArtifactDelta.DEFAULT.getMessage(), baseline, reactor, data);
}
}

private Diff computeDiff(InputStream baseline, InputStream reactor) {
Diff baselineDiff = DiffBuilder.compare(Input.fromStream(baseline))//
return DiffBuilder.compare(Input.fromStream(baseline))//
.withTest(Input.fromStream(reactor))//
.checkForSimilar()//
.ignoreComments() //
.ignoreWhitespace().build();
return baselineDiff;
}

@Override
Expand Down

0 comments on commit 5b5d2b1

Please sign in to comment.