diff --git a/v3/test/configuration_test_framework_test.go b/v3/test/configuration_test_framework_test.go index 688a2f832..06cc09ff8 100644 --- a/v3/test/configuration_test_framework_test.go +++ b/v3/test/configuration_test_framework_test.go @@ -17,6 +17,7 @@ package test import ( "fmt" "math/rand" + "os" "strconv" "sync" "testing" @@ -27,6 +28,22 @@ import ( "github.com/zmap/zlint/v3/lint" ) +func init() { + // This is a complication caused https://github.com/zmap/zlint/issues/696 + // + // This test package required access to the test certificate directory, however + // the ReadTestCert testing helper function assumes that your PWD is one of the + // lint genre directories. + // + // ReadTestCert was changed to operate from the root of the repo to accommodate this + // test package, however that broke downstream consumers who were dependent on the + // relative path building behavior. + err := os.Chdir("../lints/rfc") + if err != nil { + panic(err) + } +} + type caCommonNameMissing struct { BeerHall string Working *lint.CABFBaselineRequirementsConfig diff --git a/v3/test/helpers.go b/v3/test/helpers.go index 1f2428989..12d99adee 100644 --- a/v3/test/helpers.go +++ b/v3/test/helpers.go @@ -17,13 +17,10 @@ package test // Contains resources necessary to the Unit Test Cases import ( - "bytes" "encoding/pem" "fmt" "os" - "os/exec" - "path" "strings" "github.com/zmap/zcrypto/x509" @@ -37,6 +34,7 @@ import ( // Important: TestLint is only appropriate for unit tests. It will panic if the // lintName is not known or if the testCertFilename can not be loaded, or if the // lint result is nil. +// //nolint:revive func TestLint(lintName string, testCertFilename string) *lint.LintResult { return TestLintWithConfig(lintName, testCertFilename, "") @@ -56,6 +54,7 @@ func TestLintWithConfig(lintName string, testCertFilename string, configuration // // Important: TestLintCert is only appropriate for unit tests. It will panic if // the lintName is not known or if the lint result is nil. +// //nolint:revive func TestLintCert(lintName string, cert *x509.Certificate, ctx lint.Configuration) *lint.LintResult { l := lint.GlobalRegistry().ByName(lintName) @@ -75,24 +74,13 @@ func TestLintCert(lintName string, cert *x509.Certificate, ctx lint.Configuratio return res } -var testDir = "" - // ReadTestCert loads a x509.Certificate from the given inPath which is assumed // to be relative to `testdata/`. // // Important: ReadTestCert is only appropriate for unit tests. It will panic if // the inPath file can not be loaded. func ReadTestCert(inPath string) *x509.Certificate { - if testDir == "" { - cmd := exec.Command("git", "rev-parse", "--show-toplevel") - out, err := cmd.CombinedOutput() - if err != nil { - panic(fmt.Sprintf("error when attempting to find the root directory of the repository: %v, output: '%s'", err, out)) - } - testDir = path.Join(string(bytes.TrimSpace(out)), "v3", "testdata") - } - fullPath := path.Join(testDir, inPath) - + fullPath := fmt.Sprintf("../../testdata/%s", inPath) data, err := os.ReadFile(fullPath) if err != nil { panic(fmt.Sprintf(