Skip to content

Commit

Permalink
fix(schema): Correctly validate schema version (#2494)
Browse files Browse the repository at this point in the history
* fix(schema): Correctly validate schema version

* fix regex

* Add unit test for v2 schema

* Add v1 and v2 unit test to scala metrics

* Add pending state in strict report v2

* Add Pending deserialization to scala

* Update scala MutationTestResult schemaVersion to 2

* Update test to assert schema version 2

* Update encoder tests to schema version 2

* trim themeBackgroundColor in mutation testing elements integration test
  • Loading branch information
rouke-broersma committed May 8, 2023
1 parent e605f8d commit 72979e7
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 14 deletions.
8 changes: 4 additions & 4 deletions packages/elements/test/unit/components/app.component.spec.ts
Expand Up @@ -223,7 +223,7 @@ describe(MutationTestReportAppComponent.name, () => {
sut.element.report = createReport();
await sut.whenStable();

expect(sut.element.themeBackgroundColor).eq(' #fff');
expect(sut.element.themeBackgroundColor.trim()).eq('#fff');
});

it('should show dark theme-color on dark theme', async () => {
Expand All @@ -232,7 +232,7 @@ describe(MutationTestReportAppComponent.name, () => {
sut.element.theme = 'dark';
await sut.whenStable();

expect(sut.element.themeBackgroundColor).eq(' #18181b');
expect(sut.element.themeBackgroundColor.trim()).eq('#18181b');
});
});

Expand Down Expand Up @@ -285,7 +285,7 @@ describe(MutationTestReportAppComponent.name, () => {
sut.$('mte-theme-switch').dispatchEvent(createCustomEvent('theme-switch', 'dark'));
});
expect(event?.detail.theme).eq('dark');
expect(event?.detail.themeBackgroundColor).eq(' #18181b');
expect(event?.detail.themeBackgroundColor.trim()).eq('#18181b');
});

it('should trigger a `theme-changed` event when the theme changes during init', async () => {
Expand All @@ -296,7 +296,7 @@ describe(MutationTestReportAppComponent.name, () => {
await sut.whenStable();
});
expect(event?.detail.theme).eq('dark');
expect(event?.detail.themeBackgroundColor).eq(' #18181b');
expect(event?.detail.themeBackgroundColor.trim()).eq('#18181b');
});
});

Expand Down
Expand Up @@ -118,6 +118,7 @@ object circe {
case "CompileError" => Right(MutantStatus.CompileError)
case "RuntimeError" => Right(MutantStatus.RuntimeError)
case "Ignored" => Right(MutantStatus.Ignored)
case "Pending" => Right(MutantStatus.Pending)
case other => Left(s"Invalid status '$other'")
})(_.contramap {
case MutantStatus.Killed => "Killed"
Expand Down
Expand Up @@ -12,7 +12,7 @@ class EncoderTest extends munit.FunSuite {
val result = sut.asJson.noSpaces

val expectedJson =
"""{"$schema":"https://git.io/mutation-testing-schema","schemaVersion":"1","thresholds":{"high":80,"low":10},"projectRoot":"/src/stryker4s","files":{"src/stryker4s/Stryker4s.scala":{"source":"case class Stryker4s(foo: String)","mutants":[{"id":"1","mutatorName":"BinaryOperator","replacement":"-","location":{"start":{"line":1,"column":2},"end":{"line":2,"column":3}},"status":"Killed"}],"language":"scala"}}}"""
"""{"$schema":"https://git.io/mutation-testing-schema","schemaVersion":"2","thresholds":{"high":80,"low":10},"projectRoot":"/src/stryker4s","files":{"src/stryker4s/Stryker4s.scala":{"source":"case class Stryker4s(foo: String)","mutants":[{"id":"1","mutatorName":"BinaryOperator","replacement":"-","location":{"start":{"line":1,"column":2},"end":{"line":2,"column":3}},"status":"Killed"}],"language":"scala"}}}"""
assertNoDiff(result, expectedJson)
}

Expand All @@ -22,7 +22,7 @@ class EncoderTest extends munit.FunSuite {
val result = sut.asJson.noSpaces

val expectedJson =
"""{"$schema":"https://git.io/mutation-testing-schema","schemaVersion":"1","thresholds":{"high":80,"low":10},"files":{"src/stryker4s/Stryker4s.scala":{"source":"case class Stryker4s(foo: String)","mutants":[{"id":"1","mutatorName":"BinaryOperator","replacement":"-","location":{"start":{"line":1,"column":2},"end":{"line":2,"column":3}},"status":"Killed"}],"language":"scala"}}}"""
"""{"$schema":"https://git.io/mutation-testing-schema","schemaVersion":"2","thresholds":{"high":80,"low":10},"files":{"src/stryker4s/Stryker4s.scala":{"source":"case class Stryker4s(foo: String)","mutants":[{"id":"1","mutatorName":"BinaryOperator","replacement":"-","location":{"start":{"line":1,"column":2},"end":{"line":2,"column":3}},"status":"Killed"}],"language":"scala"}}}"""
assertNoDiff(result, expectedJson)
}

Expand All @@ -48,7 +48,7 @@ class EncoderTest extends munit.FunSuite {

test("config decoder is used") {
val report =
"""{"thresholds":{"high":80,"low":10},"files":{},"schemaVersion":"1","config":{"foo":"foovalue","bar":42}}"""
"""{"thresholds":{"high":80,"low":10},"files":{},"schemaVersion":"2","config":{"foo":"foovalue","bar":42}}"""
decode[MutationTestResult[CustomConfig]](report) match {
case Left(value) => fail(s"Expected valid decoding, got: $value")
case Right(value) =>
Expand Down
Expand Up @@ -63,7 +63,8 @@ class SchemaTest extends munit.FunSuite {
}

val validJsons = List(
"strict-report",
"strict-report-v1",
"strict-report-v2",
"additional-properties-report",
"missing-test-files",
"missing-end-location",
Expand Down
Expand Up @@ -30,7 +30,7 @@ final case class MutationTestResult[+C](
`$schema`: Option[String] = Some(
"https://git.io/mutation-testing-schema"
),
schemaVersion: String = "1",
schemaVersion: String = "2",
thresholds: Thresholds,
projectRoot: Option[String] = None,
files: FileResultDictionary,
Expand Down
Expand Up @@ -8,7 +8,7 @@ class MutationTestResultTest extends munit.FunSuite {
MutationTestResult(thresholds = Thresholds(80, 60), files = Map.empty)

assertEquals(sut.`$schema`.get, "https://git.io/mutation-testing-schema")
assertEquals(sut.schemaVersion, "1")
assertEquals(sut.schemaVersion, "2")
}

test("FileResult should have default language Scala") {
Expand Down
Expand Up @@ -12,9 +12,9 @@
},
"schemaVersion": {
"type": "string",
"pattern": "^1(\\.\\d*)?$",
"pattern": "^([1-2])(\\.(([1-9]\\d*)|0)){0,2}$",
"description": "Major version of this report. Used for compatibility.",
"examples": ["1"]
"examples": ["1", "2.0", "2.0.0"]
},
"files": {
"type": "object",
Expand Down
8 changes: 6 additions & 2 deletions packages/report-schema/test/unit/JsonValidator.spec.ts
Expand Up @@ -37,8 +37,12 @@ describe('JsonSchema', () => {
}
}

it('should validate a report that strictly complies to the schema', () => {
actAssertValid('strict-report');
it('should validate a v1 report that strictly complies to the schema', () => {
actAssertValid('strict-report-v1');
});

it('should validate a v2 report that strictly complies to the schema', () => {
actAssertValid('strict-report-v2');
});

it('should validate a report that loosely complies to the schema', () => {
Expand Down
117 changes: 117 additions & 0 deletions packages/report-schema/testResources/strict-report-v2.json
@@ -0,0 +1,117 @@
{
"$schema": "../src/mutation-testing-report-schema.json",
"schemaVersion": "2",
"thresholds": {
"high": 80,
"low": 60
},
"config": {
"custom": "config"
},
"framework": {
"name": "Stryker",
"version": "4.3.0",
"branding": {
"homepageUrl": "https://stryker-mutator.io",
"imageUrl": "https://stryker-mutator.io/img/stryker.svg"
},
"dependencies": {
"@stryker-mutator/jest-runner": "1.0.2",
"nodejs": "12",
"jest": "15.0.1"
}
},
"files": {
"foo.js": {
"language": "javascript",
"source": "function add(a, b) {\n return a + b;\n}",
"mutants": [
{
"id": "1",
"mutatorName": "Arithmetic Operator",
"location": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 13
}
},
"static": true,
"status": "Survived",
"description": "Changed + in -",
"duration": 42,
"replacement": "-",
"coveredBy": ["test-2"]
},
{
"id": "2",
"mutatorName": "Block Statement",
"location": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 3,
"column": 1
}
},
"status": "Pending",
"statusReason": "Expected 'foo' to be 'bar'",
"coveredBy": ["test-1", "test-2", "test-3"],
"killedBy": ["test-2"],
"duration": 4,
"replacement": "&&",
"testsCompleted": 2
}
]
}
},
"system": {
"ci": true,
"os": {
"description": "Ubuntu 20.04.1 LTS",
"platform": "linux",
"version": "20.04.1"
},
"cpu": {
"baseClock": 2300,
"logicalCores": 8,
"model": "Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz"
},
"ram": {
"total": 320000
}
},
"performance": {
"setup": 200,
"initialRun": 3231,
"mutation": 60321
},
"projectRoot": "/home/user/projects/project-under-test",
"testFiles": {
"test/foo.spec.js": {
"source": "describe('add', () => {})",
"tests": [
{
"id": "test-1",
"name": "foo should bar",
"location": {
"start": { "line": 4, "column": 1 },
"end": { "line": 6, "column": 1 }
}
},
{
"id": "test-2",
"name": "baz should qux",
"location": {
"start": { "line": 8, "column": 1 }
}
}
]
}
}
}

0 comments on commit 72979e7

Please sign in to comment.