Skip to content

Commit

Permalink
Add unit tests with secrets (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
hallipr authored Mar 15, 2024
1 parent 21bd3c9 commit 6d6ea78
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .dotnet/tests/OpenAI.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<!--Ignore XML doc comments on test types and members-->
<NoWarn>$(NoWarn);CS1591</NoWarn>
<LangVersion>latest</LangVersion>
Expand Down
23 changes: 23 additions & 0 deletions .dotnet/tests/TestScenarios/GitHubTests.cs
Original file line number Diff line number Diff line change
@@ -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));
}
}
17 changes: 17 additions & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
@@ -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
50 changes: 45 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: Build and Test

on:
workflow_dispatch:
push:
Expand All @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ launchSettings.json
.assets

# Build results
/artifacts
binaries/
[Dd]ebug*/
[Rr]elease/
Expand Down

0 comments on commit 6d6ea78

Please sign in to comment.