From 6d6ea7868c4537f550b33278f6e14c8595810999 Mon Sep 17 00:00:00 2001 From: Patrick Hallisey Date: Fri, 15 Mar 2024 16:12:03 -0700 Subject: [PATCH] Add unit tests with secrets (#23) --- .dotnet/tests/OpenAI.Tests.csproj | 2 +- .dotnet/tests/TestScenarios/GitHubTests.cs | 23 ++++++++++ .github/README.md | 17 ++++++++ .github/workflows/main.yml | 50 +++++++++++++++++++--- .gitignore | 1 + 5 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 .dotnet/tests/TestScenarios/GitHubTests.cs create mode 100644 .github/README.md diff --git a/.dotnet/tests/OpenAI.Tests.csproj b/.dotnet/tests/OpenAI.Tests.csproj index 9bb493c12..2556909e3 100644 --- a/.dotnet/tests/OpenAI.Tests.csproj +++ b/.dotnet/tests/OpenAI.Tests.csproj @@ -1,6 +1,6 @@ - net7.0 + net8.0 $(NoWarn);CS1591 latest diff --git a/.dotnet/tests/TestScenarios/GitHubTests.cs b/.dotnet/tests/TestScenarios/GitHubTests.cs new file mode 100644 index 000000000..3a3644f22 --- /dev/null +++ b/.dotnet/tests/TestScenarios/GitHubTests.cs @@ -0,0 +1,23 @@ +using NUnit.Framework; +using System; + +namespace OpenAI.Tests; + +public partial class GitHubTests +{ + [Test(Description = "Test that we can use a GitHub secret")] + [Category("Online")] + public void CanUseGitHubSecret() + { + string gitHubSecretString = Environment.GetEnvironmentVariable("SECRET_VALUE"); + Assert.That(gitHubSecretString, Is.Not.Null.Or.Empty); + } + + [Test(Description = "That that we can run some tests without secrets")] + [Category("Offline")] + public void CanTestWithoutSecretAccess() + { + int result = 2 + 1; + Assert.That(result, Is.EqualTo(3)); + } +} diff --git a/.github/README.md b/.github/README.md new file mode 100644 index 000000000..df5b3494c --- /dev/null +++ b/.github/README.md @@ -0,0 +1,17 @@ +The workflows in this repository try to follow existing, basic samples with little customization. + +## main.yml +We use a basic dotnet build/test/pack workflow +https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net + +- Build the solution using the dotnet cli + - Strong name the assemblies using a key stored in the repository + https://github.com/dotnet/runtime/blob/main/docs/project/strong-name-signing.md +- Test the built libraries + - Use a repository secret to hold the OpenAI token used for live testing + https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions +- Package the built libraries +- Publish the package as a GitHub Release +- Publish the package to a GitHub NuGet registry + https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-nuget-registry +- Publish a single build artifact containing test results and a nuget package \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 02d8fa18a..2d11478b2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,4 +1,5 @@ name: Build and Test + on: workflow_dispatch: push: @@ -10,6 +11,9 @@ on: jobs: build: # Test, pack and publish the Open AI nuget package as a build artifact runs-on: ubuntu-latest + env: + # if we're in a manually queued run, + version_suffix_args: ${{ format('--version-suffix="alpha.{0}"', github.run_number) }} steps: - name: Setup .NET uses: actions/setup-dotnet@v1 @@ -19,15 +23,51 @@ jobs: - name: Checkout code uses: actions/checkout@v2 + - name: Build + run: dotnet build + -c Release + ${{ env.version_suffix_args }} + working-directory: .dotnet + + - name: Test + run: dotnet test + --no-build + --configuration Release + --filter="TestCategory~${{ github.event_name == 'pull_request' && 'Offline' || 'Online' }}" + --logger "trx;LogFileName=${{github.workspace}}/artifacts/test-results/full.trx" + env: + SECRET_VALUE: ${{ secrets.OPENAI_TOKEN }} + working-directory: .dotnet + - name: Pack run: dotnet pack - -c Release - -o "${{github.workspace}}/packages" - ${{ github.ref != 'refs/heads/main' && format('--version-suffix="alpha.{0}"', github.run_number) || '' }} + --no-build + --configuration Release + --output "${{github.workspace}}/artifacts/packages" + ${{ env.version_suffix_args }} working-directory: .dotnet - name: Upload artifact uses: actions/upload-artifact@v2 with: - name: package - path: packages/*.nupkg + name: build-artifacts + path: ${{github.workspace}}/artifacts + + - name: NuGet Autenticate + if: github.event_name != 'pull_request' + run: dotnet nuget add source + "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json" + --name "github" + --username ${{ github.actor }} + --password ${{ secrets.GITHUB_TOKEN }} + --store-password-in-clear-text + working-directory: .dotnet + + - name: Publish + if: github.event_name != 'pull_request' + run: dotnet nuget push + ${{github.workspace}}/artifacts/packages/*.nupkg + --source "github" + --api-key ${{ secrets.GITHUB_TOKEN }} + --skip-duplicate + working-directory: .dotnet diff --git a/.gitignore b/.gitignore index 5a3c43f3f..f71e3dc8e 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ launchSettings.json .assets # Build results +/artifacts binaries/ [Dd]ebug*/ [Rr]elease/