Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit events when traits are changed #561

Merged
merged 3 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ public List<ValidationEvent> evaluate(Differences differences) {
changedShape.getNewShape().getType(),
Node.prettyPrintJson(oldTrait.toNode()),
Node.prettyPrintJson(newTrait.toNode()))));
} else if (oldTrait == null) {
events.add(note(changedShape.getNewShape(), newTrait, String.format(
"The `%s` trait was added to the `%s` %s shape with the value: %s",
traitName,
changedShape.getNewShape().getId(),
changedShape.getNewShape().getType(),
Node.prettyPrintJson(newTrait.toNode()))));
} else if (newTrait == null) {
events.add(warning(changedShape.getNewShape(), String.format(
"The `%s` trait was removed from the `%s` %s shape. The removed trait value was: %s",
traitName,
changedShape.getNewShape().getId(),
changedShape.getNewShape().getType(),
Node.prettyPrintJson(oldTrait.toNode()))));
} else {
events.add(note(changedShape.getNewShape(), newTrait, String.format(
"The `%s` trait was changed on the `%s` %s shape. The old trait value was: %s. "
+ "The new trait value is: %s",
traitName,
changedShape.getNewShape().getId(),
changedShape.getNewShape().getType(),
Node.prettyPrintJson(oldTrait.toNode()),
Node.prettyPrintJson(newTrait.toNode()))));
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void detectsAddedEnums() {
List<ValidationEvent> events = ModelDiff.compare(modelA, modelB);

assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").size(), equalTo(1));
assertThat(TestHelper.findEvents(events, Severity.NOTE).size(), equalTo(1));
assertThat(TestHelper.findEvents(events, "ChangedEnumTrait").get(0).getSeverity(), equalTo(Severity.NOTE));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import software.amazon.smithy.diff.ModelDiff;
Expand All @@ -33,6 +37,7 @@
import software.amazon.smithy.model.traits.DynamicTrait;
import software.amazon.smithy.model.traits.TagsTrait;
import software.amazon.smithy.model.traits.TraitDefinition;
import software.amazon.smithy.model.validation.Severity;
import software.amazon.smithy.model.validation.ValidationEvent;

public class ModifiedTraitTest {
Expand Down Expand Up @@ -98,4 +103,28 @@ private static Shape createDefinition(String tag) {
.addTrait(TraitDefinition.builder().build())
.build();
}

@Test
public void modifiedShapeNoTag() {
Model modelA = Model.assembler()
.addImport(getClass().getResource("trait-test-a.smithy"))
.assemble()
.unwrap();
Model modelB = Model.assembler()
.addImport(getClass().getResource("trait-test-b.smithy"))
.assemble()
.unwrap();
List<ValidationEvent> events = TestHelper.findEvents(ModelDiff.compare(modelA, modelB), "ModifiedTrait");
List<String> messages = events.stream()
.map(ValidationEvent::getMessage)
.collect(Collectors.toList());

assertThat(events, hasSize(3));
assertThat(events.stream().filter(e -> e.getSeverity() == Severity.WARNING).count(), equalTo(1L));
assertThat(events.stream().filter(e -> e.getSeverity() == Severity.NOTE).count(), equalTo(2L));

assertThat(messages, hasItem("The `smithy.example#c` trait was added to the `smithy.example#Foo` string shape with the value: \"foo\""));
assertThat(messages, hasItem("The `smithy.example#a` trait was removed from the `smithy.example#Foo` string shape. The removed trait value was: {}"));
assertThat(messages, hasItem("The `smithy.example#b` trait was changed on the `smithy.example#Foo` string shape. The old trait value was: \"hello\". The new trait value is: \"hello!\""));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace smithy.example

@a
@b("hello")
string Foo

@trait
structure a {}

@trait
string b

@trait
string c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace smithy.example

@b("hello!")
@c("foo")
string Foo

@trait
structure a {}

@trait
string b

@trait
string c