From 6fe9ad7e8e7a0b8381095275d0e718bab3aa8bd8 Mon Sep 17 00:00:00 2001 From: "Paul \"TBBle\" Hampson" Date: Fri, 24 Sep 2021 20:10:17 +1000 Subject: [PATCH] Check in CI that the test/ module doesn't use internal APIs An idea mentioned in-passing in #1073, this would ensure that tests can be looked at as usage examples, which use of internal APIs prevents. vendor/ and internal/ are excluded, the former because we only care about direct dependencies, and the latter so that if tests are done that depend on an unpublished API, it's possible _and_ explicit. Signed-off-by: Paul "TBBle" Hampson --- .github/workflows/ci.yml | 21 +++++++++++++++++++++ test/scripts/Verify-NoInternalImports.ps1 | 7 +++++++ 2 files changed, 28 insertions(+) create mode 100644 test/scripts/Verify-NoInternalImports.ps1 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c08533b447..5df96325a0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -60,6 +60,27 @@ jobs: } exit $process.ExitCode + verify-test-doesnt-use-internal-apis: + runs-on: 'windows-2019' + env: + GOPROXY: "https://proxy.golang.org,direct" + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-go@v2 + with: + go-version: '^1.15.0' + - name: Validate test module doesn't use internal APIs + shell: powershell + run: | + $currentPath = (Get-Location).Path + $failures = & ${currentPath}/test/scripts/Verify-NoInternalImports.ps1 "${currentPath}/test" + if ($failures.Length -gt 0) { + Write-Error ("Test module should not depend on github.com/Microsoft/hcsshim/internal/... to function." + "`r`n" + $failures) + } + exit $failures.Length + # TODO: Remove this once this test succeeds. + continue-on-error: true + test: runs-on: 'windows-2019' steps: diff --git a/test/scripts/Verify-NoInternalImports.ps1 b/test/scripts/Verify-NoInternalImports.ps1 new file mode 100644 index 0000000000..0f3005dd01 --- /dev/null +++ b/test/scripts/Verify-NoInternalImports.ps1 @@ -0,0 +1,7 @@ +$FOUND = 0 +Get-ChildItem -Path $args[0] -Recurse -Include '*.go' | Where-Object { $_.Directory -NotMatch 'vendor|internal' } | ForEach-Object { + $LISTDATA = (go list -tags functional -json $_.FullName | ConvertFrom-Json) + $INTERNALS = $LISTDATA.Imports.Where({ $_.StartsWith("github.com/Microsoft/hcsshim/internal") }) + $LISTDATA.TestImports.Where({ $_.StartsWith("github.com/Microsoft/hcsshim/internal") }) + if ($INTERNALS.Length -gt 0) { Write-Output "$_ contains one or more imports from github.com/Microsoft/hcsshim/internal/..."; $FOUND += 1 } +} +exit $FOUND