-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It wasn't a reliable way to determine if the command was "done". We need to wait for run, post-run, etc. to finish first. * Fixes #143 This commit: 1. Removes the exit channel from the CLI response emitter. 2. Adds a `Status()` function to the `cli.ResponseEmitter` as a replacement for (1). 3. Renames `Exit(code)` to `SetStatus(code)`. This new function doesn't immediately _do_ anything other than set the status. 4. Make sure to wait for _everything_ to finish before returning from `cli.Run`.
- Loading branch information
Showing
8 changed files
with
149 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ipfs/go-ipfs-cmds" | ||
) | ||
|
||
var root = &cmds.Command{ | ||
Subcommands: map[string]*cmds.Command{ | ||
"test": &cmds.Command{ | ||
Run: func(req *cmds.Request, re cmds.ResponseEmitter, e cmds.Environment) error { | ||
err := cmds.EmitOnce(re, 42) | ||
|
||
time.Sleep(2 * time.Second) | ||
|
||
e.(env).ch <- struct{}{} | ||
return err | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
type env struct { | ||
ch chan struct{} | ||
} | ||
|
||
func (e env) Context() context.Context { | ||
return context.Background() | ||
} | ||
|
||
func TestRunWaits(t *testing.T) { | ||
flag := make(chan struct{}, 1) | ||
|
||
devnull, err := os.OpenFile(os.DevNull, os.O_RDWR, 0600) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer devnull.Close() | ||
|
||
err = Run( | ||
context.Background(), | ||
root, | ||
[]string{"test", "test"}, | ||
devnull, devnull, devnull, | ||
func(ctx context.Context, req *cmds.Request) (cmds.Environment, error) { | ||
return env{flag}, nil | ||
}, | ||
func(req *cmds.Request, env interface{}) (cmds.Executor, error) { | ||
return cmds.NewExecutor(req.Root), nil | ||
}, | ||
) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
select { | ||
case <-flag: | ||
default: | ||
t.Fatal("expected flag to be raised") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.