Skip to content

Commit

Permalink
Fix for deocding issue when testplan has skipped tests in a swift tes…
Browse files Browse the repository at this point in the history
…ting suite (#46)

* testplan codable fix

* tests
  • Loading branch information
swwol authored Jan 14, 2025
1 parent 89fe1d4 commit d5e3e9d
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,45 @@ public struct LocationScenario: Codable {

public struct TestTarget: Codable {
public var parallelizable: Bool?
public var skippedTests: [String]?
public var selectedTests: [String]?
public var skippedTests: Tests?
public var selectedTests: Tests?
public var target: Target
public var enabled: Bool?
}

public enum Tests: Codable {
case array([String])
case dictionary(Suites)

public struct Suites: Codable {
let suites: [Suite]

public struct Suite: Codable {
let name: String
}
}

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let array = try? container.decode([String].self) {
self = .array(array)
return
}

if let dict = try? container.decode(Suites.self) {
self = .dictionary(dict)
return
}
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid type for skippedTests")
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .array(let array):
try container.encode(array)
case .dictionary(let dict):
try container.encode(dict)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,54 +29,6 @@ public class TestPlanHelper {
try updatedData.write(to: url)
}

static func updateSkippedTests(testPlan: inout TestPlanModel, tests: [String], override: Bool) {
checkForTestTargets(testPlan: testPlan)
for (index, _) in testPlan.testTargets.enumerated() {
if testPlan.testTargets[index].selectedTests != nil {
testPlan.testTargets[index].selectedTests = nil
}
if override {
Logger.message("Overriding skipped tests in test plan")
testPlan.testTargets[index].skippedTests = tests
} else {
if testPlan.testTargets[index].skippedTests == nil {
testPlan.testTargets[index].skippedTests = []
}
Logger.message("Append given tests to skipped tests in test plan")
testPlan.testTargets[index].skippedTests?.append(contentsOf: tests)
}
}
}

static func updateSelectedTests(testPlan: inout TestPlanModel, with tests: [String], override: Bool) {
checkForTestTargets(testPlan: testPlan)
for (index, _) in testPlan.testTargets.enumerated() {
if testPlan.testTargets[index].skippedTests != nil {
testPlan.testTargets[index].skippedTests = nil
}
if override {
Logger.message("Overriding selected tests in test plan")
testPlan.testTargets[index].selectedTests = tests
} else {
if testPlan.testTargets[index].selectedTests == nil {
testPlan.testTargets[index].selectedTests = []
}
Logger.message("Append given tests to selected tests in test plan")
testPlan.testTargets[index].selectedTests?.append(contentsOf: tests)
}
}
}

static func removeTests(testPlan: inout TestPlanModel, with tests: [String]) {
checkForTestTargets(testPlan: testPlan)
for (index, _) in testPlan.testTargets.enumerated() {
Logger.message("Remove given tests from selected tests in test plan")
for test in tests {
testPlan.testTargets[index].selectedTests?.remove(object: test)
}
}
}

static func updateRerunCount(testPlan: inout TestPlanModel, to count: Int) {
Logger.message("Updating rerun count in test plan to: \(count)")
if testPlan.defaultOptions.testRepetitionMode == nil {
Expand Down
96 changes: 96 additions & 0 deletions Tests/SelectiveTestingTests/TestPlanCodableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
@testable import TestConfigurator
import XCTest

class SkippedTestsTests: XCTestCase {
// Sample JSON for skippedTests as an array of strings
let jsonWithArray = """
{
"skippedTests": [
"DigitalRewardsServiceTests",
"LoyaltyCreditCardRewardsPointViewModelTests"
]
}
""".data(using: .utf8)!

// Sample JSON for skippedTests as a dictionary
let jsonWithDictionary = """
{
"skippedTests": {
"suites": [
{
"name": "SparksMissionOfferVisibleTrackingEventTests"
}
]
}
}
""".data(using: .utf8)!

func testDecodeSkippedTestsAsArray() throws {
let decoder = JSONDecoder()
let container = try decoder.decode(SkippedTestsContainer.self, from: jsonWithArray)

if case let .array(skippedTests) = container.skippedTests {
XCTAssertEqual(skippedTests, [
"DigitalRewardsServiceTests",
"LoyaltyCreditCardRewardsPointViewModelTests"
])
} else {
XCTFail("Expected skippedTests to be an array")
}
}

func testDecodeSkippedTestsAsDictionary() throws {
let decoder = JSONDecoder()
let container = try decoder.decode(SkippedTestsContainer.self, from: jsonWithDictionary)

if case let .dictionary(suites) = container.skippedTests {
XCTAssertEqual(suites.suites.count, 1)
XCTAssertEqual(suites.suites[0].name, "SparksMissionOfferVisibleTrackingEventTests")
} else {
XCTFail("Expected skippedTests to be a dictionary")
}
}

func testEncodeSkippedTestsAsArray() throws {
let container = SkippedTestsContainer(
skippedTests: .array([
"DigitalRewardsServiceTests",
"LoyaltyCreditCardRewardsPointViewModelTests"
])
)

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let encodedData = try encoder.encode(container)
let encodedString = String(data: encodedData, encoding: .utf8)

XCTAssertNotNil(encodedString)
XCTAssertTrue(encodedString!.contains("\"skippedTests\" : ["))
XCTAssertTrue(encodedString!.contains("\"DigitalRewardsServiceTests\""))
}

func testEncodeSkippedTestsAsDictionary() throws {
let container = SkippedTestsContainer(
skippedTests: .dictionary(
Tests.Suites(suites: [
Tests.Suites.Suite(name: "SparksMissionOfferVisibleTrackingEventTests")
])
)
)

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let encodedData = try encoder.encode(container)
let encodedString = String(data: encodedData, encoding: .utf8)

XCTAssertNotNil(encodedString)
XCTAssertTrue(encodedString!.contains("\"skippedTests\" : {"))
XCTAssertTrue(encodedString!.contains("\"suites\" : ["))
XCTAssertTrue(encodedString!.contains("\"name\" : \"SparksMissionOfferVisibleTrackingEventTests\""))
}
}

// Container to isolate the "skippedTests" field for testing
struct SkippedTestsContainer: Codable {
let skippedTests: Tests
}

0 comments on commit d5e3e9d

Please sign in to comment.