Skip to content

Commit

Permalink
Add snapshot summary and detect/remove obsolete snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor committed Aug 26, 2023
1 parent e267865 commit 97f98d5
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 91 deletions.
25 changes: 25 additions & 0 deletions src/main/java/com/askimed/nf/test/core/AbstractTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Vector;

import com.askimed.nf.test.config.Config;
import com.askimed.nf.test.lang.extensions.SnapshotFile;

public abstract class AbstractTestSuite implements ITestSuite {

Expand All @@ -24,6 +25,8 @@ public abstract class AbstractTestSuite implements ITestSuite {

private String options = "";

private SnapshotFile snapshotFile;

private List<String> tags = new Vector<String>();

@Override
Expand Down Expand Up @@ -127,4 +130,26 @@ public ITaggable getParent() {
return null;
}

@Override
public boolean hasSkippedTests() {
for (ITest test : getTests()) {
if (test.isSkipped()) {
return true;
}
}
return false;
}

@Override
public SnapshotFile getSnapshot() {
if (snapshotFile == null) {
snapshotFile = SnapshotFile.loadByTestSuite(this);
}
return snapshotFile;
}

@Override
public boolean hasSnapshotLoaded() {
return (snapshotFile != null);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

package com.askimed.nf.test.core;

import com.askimed.nf.test.lang.extensions.SnapshotFile;
import com.askimed.nf.test.util.AnsiColors;
import com.askimed.nf.test.util.AnsiText;

Expand All @@ -16,6 +17,12 @@ public class AnsiTestExecutionListener implements ITestExecutionListener {

private boolean debug = false;

private int updatedSnapshots;

private int createdSnapshots;

private int obsoleteSnapshots;

public static final int TEST_PADDING = 2;

@Override
Expand All @@ -33,6 +40,22 @@ public void testPlanExecutionFinished() {
double executionTime = ((end - start) / 1000.0);

System.out.println();

if ((updatedSnapshots + createdSnapshots + obsoleteSnapshots) > 0) {
System.out.println();
System.out.println("Snapshot Summary:");
}
if (updatedSnapshots > 0) {
System.out.println(AnsiText.padding(updatedSnapshots + " updated", TEST_PADDING));
}
if (createdSnapshots > 0) {
System.out.println(AnsiText.padding(createdSnapshots + " created", TEST_PADDING));
}

if (obsoleteSnapshots > 0) {
System.out.println(AnsiColors.yellow(AnsiText.padding(obsoleteSnapshots + " obsolete", TEST_PADDING)));
}

System.out.println();

if (failed > 0) {
Expand Down Expand Up @@ -63,6 +86,46 @@ public void testSuiteExecutionStarted(ITestSuite testSuite) {
@Override
public void testSuiteExecutionFinished(ITestSuite testSuite) {

if (!testSuite.hasSnapshotLoaded()) {
return;
}

SnapshotFile snapshot = testSuite.getSnapshot();
if ((snapshot.getUpdatedSnapshots().size() + snapshot.getCreatedSnapshots().size()
+ snapshot.getObsoleteSnapshots().size()) > 0 || testSuite.hasSkippedTests()) {
System.out.println(AnsiText.padding("Snapshots:", TEST_PADDING));
}
if (snapshot.getUpdatedSnapshots().size() > 0) {
System.out.println(AnsiText.padding(
snapshot.getUpdatedSnapshots().size() + " updated " + snapshot.getUpdatedSnapshots(),
2 * TEST_PADDING));
}
if (snapshot.getCreatedSnapshots().size() > 0) {
System.out.println(AnsiText.padding(
snapshot.getCreatedSnapshots().size() + " created " + snapshot.getCreatedSnapshots(),
2 * TEST_PADDING));
}

updatedSnapshots += snapshot.getUpdatedSnapshots().size();
createdSnapshots += snapshot.getCreatedSnapshots().size();

// if we have at least one skipped test, we can not
// determine if a snapshot is obsolete.
// TODO: add check. if at lest one test failed, same situation.
if (testSuite.hasSkippedTests()) {
System.out.println(AnsiText.padding(
"Obsolete snapshots can only be checked if all tests of a file are executed.", 2 * TEST_PADDING));
return;
}

if (snapshot.getObsoleteSnapshots().size() > 0) {
System.out.println(AnsiColors.yellow(AnsiText.padding(
snapshot.getObsoleteSnapshots().size() + " obsolete " + snapshot.getObsoleteSnapshots(),
2 * TEST_PADDING)));
}

obsoleteSnapshots += snapshot.getObsoleteSnapshots().size();

}

@Override
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/askimed/nf/test/core/ITestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import com.askimed.nf.test.config.Config;
import com.askimed.nf.test.lang.extensions.SnapshotFile;

public interface ITestSuite extends ITaggable {

Expand All @@ -20,5 +21,11 @@ public interface ITestSuite extends ITaggable {
public String getFilename();

public void configure(Config config);

public boolean hasSkippedTests();

public SnapshotFile getSnapshot();

public boolean hasSnapshotLoaded();

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Vector;

import com.askimed.nf.test.lang.TestSuiteBuilder;
import com.askimed.nf.test.lang.extensions.SnapshotFile;
import com.askimed.nf.test.plugins.PluginManager;
import com.askimed.nf.test.util.AnsiColors;
import com.askimed.nf.test.util.AnsiText;
Expand Down Expand Up @@ -202,6 +203,14 @@ public int execute() throws Throwable {

}

// Remove obsolete snapshots in update mode and when no test was skipped.
//TODO: removeObsolete snapshots only when no test failed!!
if (updateSnapshot && !testSuite.hasSkippedTests() && testSuite.hasSnapshotLoaded()) {
SnapshotFile snapshot = testSuite.getSnapshot();
snapshot.removeObsoleteSnapshots();
snapshot.save();
}

listener.testSuiteExecutionFinished(testSuite);

}
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/com/askimed/nf/test/lang/extensions/Snapshot.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.askimed.nf.test.lang.extensions;

import java.util.Date;
import java.io.IOException;

import com.askimed.nf.test.core.ITest;

Expand All @@ -14,28 +14,29 @@ public class Snapshot {

public Snapshot(Object actual, ITest test) {
this.actual = actual;
this.file = SnapshotFile.loadByTestSuite(test.getTestSuite());
this.file = test.getTestSuite().getSnapshot();
this.test = test;
}

public boolean match() {
public boolean match() throws IOException {
return match(test.getName());
}

public boolean match(String id) {
public boolean match(String id) throws IOException {
SnapshotFileItem expected = file.getSnapshot(id);
//new snapshot --> create snapshot
if (expected == null) {
file.updateSnapshot(id, actual);
file.createSnapshot(id, actual);
file.save();
return true;
}

try {
return new SnapshotFileItem(new Date(), actual).equals(expected);
//compare actual snapshot with expected
return new SnapshotFileItem(actual).equals(expected);
} catch (Exception e) {
// test failes
// test failes and flag set --> update snapshot
if (test.isUpdateSnapshot()) {
// udpate snapshot
file.updateSnapshot(id, actual);
file.save();
return true;
Expand All @@ -45,9 +46,9 @@ public boolean match(String id) {
}

}

public void view() {

}

}
Loading

0 comments on commit 97f98d5

Please sign in to comment.