-
Notifications
You must be signed in to change notification settings - Fork 1
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
Make changes to support testing individual commands, add a few tests #15
Conversation
Trying to make it an injected thing for easier testing.
@@ -2,17 +2,25 @@ name: "go-linter" | |||
|
|||
on: | |||
pull_request: | |||
types: [opened, synchronize, reopened] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Run the linter on fewer PR events, such as when the PR is labelled.
merge_group: | ||
workflow_dispatch: | ||
push: | ||
branches: | ||
- 'main' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also lint what gets merged into the main branch.
|
||
permissions: | ||
contents: read | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | ||
cancel-in-progress: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cancel an existing run of the linter for the same commit.
jobs: | ||
lint: | ||
strategy: | ||
fail-fast: false | ||
runs-on: ubuntu-latest-xl | ||
runs-on: ubuntu-latest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably don't need the XL version.
Shorter lines, a little less reaching through.
cmd/root.go
Outdated
token, _ := auth.TokenForHost("github.com") | ||
if token == "" { | ||
util.WriteToOut(out, "No GitHub token found. Please run 'gh auth login' to authenticate.\n") | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each of the commands (run, view, list) was doing this, so I pulled out to this earlier point to DRY things up. It also lets me test the individual commands by passing in a mock client.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does moving this affect the ability to list commands, get help from the cli unless you're signed in?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It did indeed affect things! Fixed with 1b48b0f. Now when I'm not authenticated and I try to run a command, I get output like:
% ./gh-models view gpt-4o-mini
No GitHub token found. Please run 'gh auth login' to authenticate.
Error: not authenticated
Usage:
gh models view [model] [flags]
Flags:
-h, --help help for view
If I just try to check usage info, I get output like:
% ./gh-models list --help
No GitHub token found. Please run 'gh auth login' to authenticate.
List available models
Usage:
gh models list [flags]
Flags:
-h, --help help for list
Less reaching-through at call sites.
} | ||
|
||
func (p *modelPrinter) render() error { | ||
modelSummary := p.modelSummary | ||
if modelSummary != nil { | ||
p.printLabelledLine("Display name:", modelSummary.FriendlyName) | ||
p.printLabelledLine("Summary name:", modelSummary.Name) | ||
p.printLabelledLine("Model name:", modelSummary.Name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Writing the 'view' command test showed me this in the output produced, making me realize we were labelling this field wrong. Yay for tests! 🎉
@@ -36,7 +36,8 @@ type ChatCompletionOptions struct { | |||
TopP *float64 `json:"top_p,omitempty"` | |||
} | |||
|
|||
type chatChoiceMessage struct { | |||
// ChatChoiceMessage is a message from a choice in a chat conversation. | |||
type ChatChoiceMessage struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made public for use in tests.
.github/workflows/test.yml
Outdated
- name: Configure Go private module access | ||
run: | | ||
echo "machine goproxy.githubapp.com login nobody password ${{ secrets.GOPROXY_TOKEN }}" >> $HOME/.netrc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we use - uses: github/[email protected]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quite possibly! Let me try that out, this was copy-pasta from another github-org Go app.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dropped from both our test and lint workflows in 3ede78e, we'll see if anything goes awry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, both builds are fine! 🎉
cmd/root.go
Outdated
token, _ := auth.TokenForHost("github.com") | ||
if token == "" { | ||
util.WriteToOut(out, "No GitHub token found. Please run 'gh auth login' to authenticate.\n") | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does moving this affect the ability to list commands, get help from the cli unless you're signed in?
internal/azuremodels/client.go
Outdated
// GetChatCompletionStream returns a stream of chat completions using the given options. | ||
GetChatCompletionStream(context.Context, ChatCompletionOptions) (*ChatCompletionResponse, error) | ||
// GetModelDetails returns the details of the specified model in a particular registry. | ||
GetModelDetails(context.Context, string, string, string) (*ModelDetails, error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, use names for the parameters to understand which 3 strings this takes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! Fixed in 09cd4ae.
e.g., ./gh-models run gpt-4o-mini "hello" No GitHub token found. Please run 'gh auth login' to authenticate. panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xba48c6a] goroutine 1 [running]: github.com/github/gh-models/internal/azuremodels.(*AzureClient).ListModels(0x0, {0xd098300, 0xd5ca4a0}) gh-models/internal/azuremodels/azure_client.go:194 +0x20a github.com/github/gh-models/cmd/run.(*runCommandHandler).loadModels(0x4?) gh-models/cmd/run/run.go:382 +0x2a github.com/github/gh-models/cmd/run.NewRunCommand.func1(0xc00021a308, {0xc0002d82e0, 0x2, 0x2}) gh-models/cmd/run/run.go:203 +0x139 github.com/spf13/cobra.(*Command).execute(0xc00021a308, {0xc0002d82a0, 0x2, 0x2}) go/pkg/mod/github.com/spf13/[email protected]/command.go:985 +0xaca github.com/spf13/cobra.(*Command).ExecuteC(0xc0001fd808) go/pkg/mod/github.com/spf13/[email protected]/command.go:1117 +0x3ff github.com/spf13/cobra.(*Command).ExecuteContextC(0xb6c5305?, {0xd098300?, 0xd5ca4a0?}) go/pkg/mod/github.com/spf13/[email protected]/command.go:1050 +0x47 main.mainRun() gh-models/main.go:29 +0x26 main.main() gh-models/main.go:19 +0x13
https: //github.com//pull/15#discussion_r1797368843 Co-Authored-By: Christopher Schleiden <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for refactoring this to make it testable. One nit around changed token validation behavior, not a showstopper.
|
||
var client azuremodels.Client | ||
|
||
if token == "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For gh models run
, previously the code would have immediately bailed out of the command if you had no token set, but now it looks like it's going to try and keep going with a ""
token (and eventually run into some 403 error).
I really like that this PR still lets you do some commands without a token, but do you think it's worth trying to preserve the old behavior for run
(printing a human-readable error and immediately exiting)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think it's worth trying to preserve the old behavior for run (printing a human-readable error and immediately exiting)?
So the effect feels the same to users. When I try to run
as an unauthenticated user right now on this branch, here's what I get:
% ./gh-models run gpt-4o-mini "hello world"
No GitHub token found. Please run 'gh auth login' to authenticate.
Error: not authenticated
Usage:
gh models run [model] [prompt] [flags]
Flags:
-h, --help help for run
--max-tokens string Limit the maximum tokens for the model response.
--system-prompt string Prompt the system.
--temperature string Controls randomness in the response, use lower to be more deterministic.
--top-p string Controls text diversity by selecting the most probable words until a set probability is reached.
Are you worried about us trying to make an HTTP call with an invalid (blank) token?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really mind (actually making the HTTP call is not a problem), I was just a little worried that the error would be ugly. Given that it isn't, I have no qualms here - no need to follow up in another branch.
I landed the PR so we can start having some test coverage and help avoid regressions/breakages, Sean, but reply back above regarding the blank token and I can iterate further in a follow-up branch! |
For https://github.com/github/models/issues/330. This adds a few basic tests covering the happy path for the view, run, and list commands. It also adds a new Actions workflow for running tests.
To support testing without hitting the actual Azure APIs, I added a new
command.Config
type to bundle together an Azure client and output streams. I renamedazuremodels.Client
toazuremodels.AzureClient
and made a newazuremodels.Client
that's an interface. That let me add anazuremodels.MockClient
type for use in tests, allowing us to stub out functions likeListModels
to avoid making real HTTP requests.Current test output: