Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more test coverage, stop swallowing client error #18

Merged
merged 12 commits into from
Oct 15, 2024
Merged

Conversation

cheshire137
Copy link
Member

@cheshire137 cheshire137 commented Oct 14, 2024

This adds tests for the --help output of commands as well as for azuremodels.AzureClient's GetModelDetails.

To make azuremodels.AzureClient more testable, I switched it from using constants for API URLs to instead taking a config object that provides the URLs to use. That lets us in tests start a test server and use that server as a stand-in for third-party APIs.

@cheshire137 cheshire137 self-assigned this Oct 14, 2024
@@ -28,7 +28,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ vars.GOVERSION }}
go-version: ">=1.22"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifying the same Go version here as we do in our dev docs.

@@ -43,4 +43,6 @@ jobs:
run: go build -v ./...

- name: Run tests
run: go test -race -cover ./...
run: |
go version
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outputting the version for visibility when looking at test results, in case the version affects the results.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement! We could include this in a script/test as well, for consistency with other repos.

@@ -32,7 +32,12 @@ func NewRootCommand() *cobra.Command {
util.WriteToOut(out, "No GitHub token found. Please run 'gh auth login' to authenticate.\n")
client = azuremodels.NewUnauthenticatedClient()
} else {
client = azuremodels.NewAzureClient(token)
var err error
client, err = azuremodels.NewDefaultAzureClient(token)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting together an AzureClient requires getting an http.Client, which could error. We're no longer silencing that error if it occurs, which helped me figure out a panic occurring on CI when running tests.

@@ -98,37 +98,43 @@ func (m *ModelSummary) HasName(name string) bool {
return strings.EqualFold(m.FriendlyName, name) || strings.EqualFold(m.Name, name)
}

type modelCatalogTextLimits struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pulled out these nested types for ease of reference in testing.


// NewAzureClient returns a new Azure client using the given auth token.
func NewAzureClient(authToken string) *AzureClient {
httpClient, _ := api.DefaultHTTPClient()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the error we were swallowing before.

@cheshire137 cheshire137 changed the title Add more test coverage Add more test coverage, stop swallowing client error Oct 15, 2024
@cheshire137 cheshire137 marked this pull request as ready for review October 15, 2024 16:16
@cheshire137 cheshire137 requested a review from a team as a code owner October 15, 2024 16:16

require.NoError(t, err)
require.Contains(t, outBuf.String(), "List available models")
require.Equal(t, "", errBuf.String())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require.Equal(t, "", errBuf.String())
require.Empty(t, errBuf.String())

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice!

Co-Authored-By: Christopher Schleiden <[email protected]>
@cheshire137 cheshire137 merged commit 6a7b0dc into main Oct 15, 2024
5 checks passed
@cheshire137 cheshire137 deleted the moar-tests branch October 15, 2024 16:35
@cheshire137
Copy link
Member Author

Forgot to mention, but this is the test coverage with this branch's changes:

% go test -race -cover ./...
        github.com/github/gh-models             coverage: 0.0% of statements
ok      github.com/github/gh-models/cmd (cached)        coverage: 78.9% of statements
ok      github.com/github/gh-models/cmd/list    (cached)        coverage: 93.1% of statements
ok      github.com/github/gh-models/cmd/run     (cached)        coverage: 47.2% of statements
ok      github.com/github/gh-models/cmd/view    (cached)        coverage: 80.0% of statements
ok      github.com/github/gh-models/internal/azuremodels        (cached)        coverage: 27.5% of statements
        github.com/github/gh-models/internal/sse                coverage: 0.0% of statements
        github.com/github/gh-models/pkg/command         coverage: 0.0% of statements
        github.com/github/gh-models/pkg/util            coverage: 0.0% of statements
ok      github.com/github/gh-models/internal/ux (cached)        coverage: 72.7% of statements

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants