Skip to content

Commit

Permalink
fix(remove): Flag --all as an alias for --revisions=all. More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Jan 14, 2019
1 parent 7888a2c commit 7e29002
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 11 deletions.
85 changes: 85 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,86 @@ func TestRemoveOnlyTwoRevisions(t *testing.T) {
}
}

// Test that adding three revision, then removing all of them leaves nothing.
func TestRemoveAllRevisionsLongForm(t *testing.T) {
if err := confirmQriNotRunning(); err != nil {
t.Skip(err.Error())
}

r := NewTestRepoRoot(t, "qri_test_remove_only_one_revision")
defer r.Delete()

cmdR := r.CreateCommandRunner()
_, err := executeCommand(cmdR, "qri save --body=testdata/movies/body_ten.csv me/test_movies")
if err != nil {
t.Fatalf(err.Error())
}

cmdR = r.CreateCommandRunner()
_, err = executeCommand(cmdR, "qri save --body=testdata/movies/body_twenty.csv me/test_movies")
if err != nil {
t.Fatalf(err.Error())
}

cmdR = r.CreateCommandRunner()
_, err = executeCommand(cmdR, "qri save --body=testdata/movies/body_thirty.csv me/test_movies")
if err != nil {
t.Fatalf(err.Error())
}

cmdR = r.CreateCommandRunner()
_, err = executeCommand(cmdR, "qri remove me/test_movies --revisions=all")
if err != nil {
t.Fatalf(err.Error())
}

// Read path for dataset, which shouldn't exist anymore.
dsPath := r.GetPathForDataset(0)
if dsPath != "" {
t.Errorf("expected dataset to be removed entirely, found at \"%s\"", dsPath)
}
}

// Test that adding three revision, then removing all of them leaves nothing, using --all.
func TestRemoveAllRevisionsShortForm(t *testing.T) {
if err := confirmQriNotRunning(); err != nil {
t.Skip(err.Error())
}

r := NewTestRepoRoot(t, "qri_test_remove_only_one_revision")
defer r.Delete()

cmdR := r.CreateCommandRunner()
_, err := executeCommand(cmdR, "qri save --body=testdata/movies/body_ten.csv me/test_movies")
if err != nil {
t.Fatalf(err.Error())
}

cmdR = r.CreateCommandRunner()
_, err = executeCommand(cmdR, "qri save --body=testdata/movies/body_twenty.csv me/test_movies")
if err != nil {
t.Fatalf(err.Error())
}

cmdR = r.CreateCommandRunner()
_, err = executeCommand(cmdR, "qri save --body=testdata/movies/body_thirty.csv me/test_movies")
if err != nil {
t.Fatalf(err.Error())
}

cmdR = r.CreateCommandRunner()
_, err = executeCommand(cmdR, "qri remove me/test_movies --all")
if err != nil {
t.Fatalf(err.Error())
}

// Read path for dataset, which shouldn't exist anymore.
dsPath := r.GetPathForDataset(0)
if dsPath != "" {
t.Errorf("expected dataset to be removed entirely, found at \"%s\"", dsPath)
}
}

// TODO: Perhaps this utility should move to a lower package, and be used as a way to validate the
// bodies of dataset in more of our test case. That would require extracting some parts out, like
// pathFactory, which would probably necessitate the pathFactory taking the testRepoRoot as a
Expand Down Expand Up @@ -386,6 +466,11 @@ func (r *TestRepoRoot) GetPathForDataset(index int) string {
r.t.Fatal(err)
}

// If dataset doesn't exist, return an empty string for the path.
if len(result) == 0 {
return ""
}

var dsPath string
dsPath = result[index]["path"].(string)
return dsPath
Expand Down
31 changes: 20 additions & 11 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ adjust this cap using IPFS, qri will respect it.
In the future we’ll add a flag that’ll force immediate removal of a dataset from
both qri & IPFS. Promise.`,
Example: ` remove a dataset named annual_pop:
$ qri remove me/annual_pop --revisions=all`,
$ qri remove me/annual_pop --all`,
Annotations: map[string]string{
"group": "dataset",
},
Expand All @@ -43,6 +43,7 @@ both qri & IPFS. Promise.`,
}

cmd.Flags().StringVarP(&o.RevisionsText, "revisions", "r", "", "revisions to delete")
cmd.Flags().BoolVarP(&o.All, "all", "a", false, "synonym for --revisions=all")

return cmd
}
Expand All @@ -54,6 +55,7 @@ type RemoveOptions struct {
Args []string

RevisionsText string
All bool
Revision rev.Rev

DatasetRequests *lib.DatasetRequests
Expand All @@ -66,17 +68,24 @@ func (o *RemoveOptions) Complete(f Factory, args []string) (err error) {
if err != nil {
return err
}
if o.RevisionsText == "" {
return fmt.Errorf("--revisions flag is requried")
}
revisions, err := rev.ParseRevs(o.RevisionsText)
if len(revisions) != 1 {
return fmt.Errorf("need exactly 1 revision parameter to remove")
}
if revisions[0] == nil {
return fmt.Errorf("invalid nil revision")
if o.All {
o.Revision = rev.NewAllRevisions()
} else {
if o.RevisionsText == "" {
return fmt.Errorf("--revisions flag is requried")
}
revisions, err := rev.ParseRevs(o.RevisionsText)
if err != nil {
return err
}
if len(revisions) != 1 {
return fmt.Errorf("need exactly 1 revision parameter to remove")
}
if revisions[0] == nil {
return fmt.Errorf("invalid nil revision")
}
o.Revision = *revisions[0]
}
o.Revision = *revisions[0]
return err
}

Expand Down
5 changes: 5 additions & 0 deletions rev/rev.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func ParseRev(rev string) (*Rev, error) {
return nil, fmt.Errorf("unrecognized revision field: %s", rev)
}

// NewAllRevisions returns a Rev struct that represents all revisions.
func NewAllRevisions() Rev {
return Rev{Field: "ds", Gen: AllGenerations}
}

var fieldMap = map[string]string{
"dataset": "ds",
"meta": "md",
Expand Down

0 comments on commit 7e29002

Please sign in to comment.