Skip to content

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
- 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.

- 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.

} 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.

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 <cschleiden@live.de>
@cheshire137 cheshire137 enabled auto-merge October 15, 2024 16:34
@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.

4 participants