import Foundation @testable import PureYAML import Testing @Suite("Real YAML Fixtures") struct RealYAMLFixtureTests { @Test("Real YAML fixture files match their pinned manifest", arguments: RealYAMLFixture.allCases) func test_realYAMLFixtureFilesMatchPinnedManifest(fixture: RealYAMLFixture) throws { let source = try fixture.load() #expect(source.utf8.count == fixture.byteCount) #expect(source.count(where: { $0 == "\n" }) == fixture.lineCount) for requiredSubstring in fixture.requiredSubstrings { #expect(source.contains(requiredSubstring)) } for forbiddenSubstring in fixture.forbiddenSubstrings { #expect(!source.contains(forbiddenSubstring)) } } @Test("Parses real YAML fixtures with exact representative values", arguments: RealYAMLFixture.allCases) func test_parsesRealYAMLFixturesWithExactRepresentativeValues(fixture: RealYAMLFixture) throws { let value = try PureYAML.parse(fixture.load()) fixture.expectRepresentativeValues(in: value) #expect(PureYAML.Validation.Validator().collect(value) == PureYAML.Validation.Result()) } } enum RealYAMLFixture: String, CaseIterable, CustomStringConvertible { case certManagerCertificateCRD = "cert-manager-certificate-crd.yaml" case certManagerValues = "cert-manager-values.yaml" case dockerComposeNginxGolangPostgres = "docker-compose-nginx-golang-postgres.yaml" case githubActionsSwiftFormat = "github-actions-swift-format.yml" case githubRestAPI = "github-rest-api.yaml" case kubernetesSimplePod = "kubernetes-simple-pod.yaml" case kustomizeWordPress = "kustomize-wordpress.yaml" case openAPIPetstore = "openapi-petstore.yaml" case prometheusGoodConfig = "prometheus-conf-good.yml" case prowClusterAPIPresubmits = "prow-cluster-api-presubmits.yaml" var description: String { rawValue } var lineCount: Int { switch self { case .certManagerCertificateCRD: 883 case .certManagerValues: 1759 case .dockerComposeNginxGolangPostgres: 48 case .githubActionsSwiftFormat: 84 case .githubRestAPI: 257_280 case .kubernetesSimplePod: 12 case .kustomizeWordPress: 18 case .openAPIPetstore: 119 case .prometheusGoodConfig: 481 case .prowClusterAPIPresubmits: 427 } } var byteCount: Int { switch self { case .certManagerCertificateCRD: 44059 case .certManagerValues: 65536 case .dockerComposeNginxGolangPostgres: 852 case .githubActionsSwiftFormat: 3591 case .githubRestAPI: 9_549_118 case .kubernetesSimplePod: 186 case .kustomizeWordPress: 262 case .openAPIPetstore: 2768 case .prometheusGoodConfig: 11562 case .prowClusterAPIPresubmits: 14213 } } var requiredSubstrings: [String] { switch self { case .certManagerCertificateCRD: [ "apiVersion: apiextensions.k8s.io/v1", "jsonPath: .status.conditions[?(@.type == \"Ready\")].status", "Known condition types are `Ready` and `Issuing`.", ] case .certManagerValues: [ "installCRDs: false", "hostUsers: false", "webhook:", ] case .dockerComposeNginxGolangPostgres: [ "services:", "test: [ \"CMD\", \"pg_isready\" ]", "file: db/password.txt", ] case .githubActionsSwiftFormat: [ "name: Pull request", "types: [opened, reopened, synchronize, ready_for_review]", "run: |", ] case .githubRestAPI: [ "openapi: 3.1.0", "title: GitHub v3 REST API", "components:", ] case .kubernetesSimplePod: [ "kind: Pod", "name: pods-simple-container", ] case .kustomizeWordPress: [ "namePrefix: demo-", "name: WORDPRESS_SERVICE", ] case .openAPIPetstore: [ "title: Swagger Petstore", "operationId: listPets", "'201':", ] case .prometheusGoodConfig: [ "scrape_interval: 15s", "fallback_scrape_protocol: PrometheusText0.0.4", "password: \"multiline\\nmysecret\\ntest\"", ] case .prowClusterAPIPresubmits: [ "presubmits:", "run_if_changed: '^((api|bootstrap|cmd|config|controllers|controlplane|errors|exp|feature|hack|internal|scripts|test|util" + "|webhooks|version)/|main\\.go|go\\.mod|go\\.sum|Dockerfile|Makefile)'", "securityContext:", ] } } var forbiddenSubstrings: [String] { [ "<<<<<<<", "=======", ">>>>>>>", ] } func load() throws -> String { let url = try #require(Bundle.module.url( forResource: resourceName, withExtension: resourceExtension, subdirectory: "Fixtures/real-yaml", )) return try String(contentsOf: url, encoding: .utf8) } func expectRepresentativeValues(in value: PureYAML.Model.Value) { switch self { case .certManagerCertificateCRD: expectCertManagerCertificateCRD(value) case .certManagerValues: expectCertManagerValues(value) case .dockerComposeNginxGolangPostgres: expectDockerComposeNginxGolangPostgres(value) case .githubActionsSwiftFormat: expectGitHubActionsSwiftFormat(value) case .githubRestAPI: expectGitHubRestAPI(value) case .kubernetesSimplePod: expectKubernetesSimplePod(value) case .kustomizeWordPress: expectKustomizeWordPress(value) case .openAPIPetstore: expectOpenAPIPetstore(value) case .prometheusGoodConfig: expectPrometheusGoodConfig(value) case .prowClusterAPIPresubmits: expectProwClusterAPIPresubmits(value) } } private var resourceName: String { guard let dotIndex = rawValue.lastIndex(of: ".") else { return rawValue } return String(rawValue[..