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

cmd/snap: fix snap run command auto completion #11315

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

patriciadomin
Copy link

LP: #1844776

@anonymouse64 anonymouse64 changed the title lp1844776 fix snap run command auto completion cmd/snap: fix snap run command auto completion Jan 27, 2022
@codecov-commenter
Copy link

codecov-commenter commented Jan 27, 2022

Codecov Report

Merging #11315 (3dc8e0b) into master (44fd502) will increase coverage by 0.01%.
The diff coverage is 41.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #11315      +/-   ##
==========================================
+ Coverage   78.30%   78.31%   +0.01%     
==========================================
  Files         930      930              
  Lines      106430   106464      +34     
==========================================
+ Hits        83344    83382      +38     
+ Misses      17908    17902       -6     
- Partials     5178     5180       +2     
Flag Coverage Δ
unittests 78.31% <41.50%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
cmd/snap/complete.go 53.56% <26.08%> (-1.93%) ⬇️
cmd/snap/main.go 73.40% <43.47%> (+6.16%) ⬆️
cmd/snap/cmd_run.go 60.64% <85.71%> (+0.12%) ⬆️
store/cache.go 69.23% <0.00%> (-1.93%) ⬇️
cmd/snap/cmd_debug_state.go 70.64% <0.00%> (+0.45%) ⬆️
overlord/hookstate/hookmgr.go 75.32% <0.00%> (+0.64%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 44fd502...3dc8e0b. Read the comment docs.

…/snap

Also add unit tests for the two situations from main() of cmd/snap/main.go that
have special handling since we apparently did not have any unit test coverage
there before.

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>
@anonymouse64
Copy link
Member

Thanks for working on this, it seems we have some missing unit tests for cmd/snap/main.go, hence why you didn't catch or notice that this doesn't work for the case where snap run is executed via the symlinks from /snap/bin/hello-world -> /usr/bin/snap. I have a commit here which fixes this and also adds the unit tests we were missing that should fix all of the broken unit tests (which amazingly seems to be all of them 😺 ) anonymouse64@24d439d

Let me know if you would like me to push this commit to this branch here

@patriciadomin
Copy link
Author

Thanks for working on this, it seems we have some missing unit tests for cmd/snap/main.go, hence why you didn't catch or notice that this doesn't work for the case where snap run is executed via the symlinks from /snap/bin/hello-world -> /usr/bin/snap. I have a commit here which fixes this and also adds the unit tests we were missing that should fix all of the broken unit tests (which amazingly seems to be all of them smiley_cat ) anonymouse64@24d439d

Let me know if you would like me to push this commit to this branch here

Yes, please. Thanks

Copy link
Member

@anonymouse64 anonymouse64 left a comment

Choose a reason for hiding this comment

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

lgtm, but since I pushed some changes here, others also need to approve

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>
Copy link
Collaborator

@bboozzoo bboozzoo left a comment

Choose a reason for hiding this comment

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

Thanks!


func (s installedCommand) Complete(match string) []flags.Completion {
cli := mkClient()
apps, err := cli.Apps(nil, client.AppOptions{Service: false})
Copy link
Collaborator

Choose a reason for hiding this comment

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

The only downside I see is that this will make snap do and load snap.yaml from each installed snap. Maybe we should consider being smarter completion of apps at some point, similarly to what we have for snap advise. In any way, this would be a followup material as it would probably need some discussion.

Copy link
Member

Choose a reason for hiding this comment

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

True, it is loading snap.yaml from every installed snap, but it is only doing this when it needs to complete

@bboozzoo
Copy link
Collaborator

bboozzoo commented Feb 3, 2022

@patriciadomin There were some conflicts with the master branch, so I merged master to this branch and pushed to your repo, hope it's ok.

Copy link
Contributor

@stolowski stolowski left a comment

Choose a reason for hiding this comment

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

Great job, thank you! Works as advertised in my manual testing, let's see how spread test like it. +1

Copy link
Contributor

@MiguelPires MiguelPires left a comment

Choose a reason for hiding this comment

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

Just some testing related questions

Comment on lines +73 to +92
snaps := map[string]int{}
var ret []flags.Completion
for _, app := range apps {
if app.IsService() {
continue
}
name := snap.JoinSnapApp(app.Snap, app.Name)
if !strings.HasPrefix(name, match) {
continue
}
ret = append(ret, flags.Completion{Item: name})
if len(match) <= len(app.Snap) {
snaps[app.Snap]++
}
}
for snap, n := range snaps {
if n > 1 {
ret = append(ret, flags.Completion{Item: snap})
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAICT, there are no tests checking the completion logic. Could we write some?

c.Assert(r.Method, check.Equals, "GET")
EncodeResponseBody(c, w, map[string]interface{}{
"type": "sync",
"result": fortestingConnectionList,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this needs to return something that the completion logic can iterate through. As it is, this will fail to be deserialized and not exercise the completion logic

@pedronis pedronis self-assigned this Aug 11, 2023
@ernestl ernestl removed the Bug label Dec 8, 2023
@ernestl
Copy link
Collaborator

ernestl commented Feb 27, 2024

Hi @patriciadomin, this work is still useful to us, would you like to continue?

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