Skip to content

Commit

Permalink
global: refactor package structure of commands and testdata
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoRosendo committed Aug 31, 2022
1 parent 93cf70b commit 6c1147e
Show file tree
Hide file tree
Showing 88 changed files with 1,579 additions and 1,427 deletions.
17 changes: 9 additions & 8 deletions cmd/close.go → cmd/close/close.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ REANA is free software; you can redistribute it and/or modify it
under the terms of the MIT License; see LICENSE file for more details.
*/

package cmd
// Package close provides the command to close an interactive session.
package close

import (
"fmt"
Expand All @@ -18,7 +19,7 @@ import (
"github.com/spf13/cobra"
)

const closeDesc = `
const description = `
Close an interactive session.
The ` + "``close``" + ` command allows to shut down any interactive sessions that you
Expand All @@ -31,19 +32,19 @@ Examples:
$ reana-client close -w myanalysis.42
`

type closeOptions struct {
type options struct {
token string
workflow string
}

// newCloseCmd creates a command to close an interactive session.
func newCloseCmd() *cobra.Command {
o := &closeOptions{}
// NewCmd creates a command to close an interactive session.
func NewCmd() *cobra.Command {
o := &options{}

cmd := &cobra.Command{
Use: "close",
Short: "Close an interactive session.",
Long: closeDesc,
Long: description,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(cmd)
Expand All @@ -62,7 +63,7 @@ func newCloseCmd() *cobra.Command {
return cmd
}

func (o *closeOptions) run(cmd *cobra.Command) error {
func (o *options) run(cmd *cobra.Command) error {
closeParams := operations.NewCloseInteractiveSessionParams()
closeParams.SetAccessToken(&o.token)
closeParams.SetWorkflowIDOrName(o.workflow)
Expand Down
29 changes: 15 additions & 14 deletions cmd/close_test.go → cmd/close/close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ REANA is free software; you can redistribute it and/or modify it
under the terms of the MIT License; see LICENSE file for more details.
*/

package cmd
package close

import (
"fmt"
"net/http"
. "reanahub/reana-client-go/cmd/internal"
"testing"
)

Expand All @@ -19,34 +20,34 @@ var closePathTemplate = "/api/workflows/%s/close/"
func TestClose(t *testing.T) {
tests := map[string]TestCmdParams{
"success": {
serverResponses: map[string]ServerResponse{
ServerResponses: map[string]ServerResponse{
fmt.Sprintf(closePathTemplate, "my_workflow"): {
statusCode: http.StatusOK,
responseFile: "empty.json",
StatusCode: http.StatusOK,
ResponseFile: "../../testdata/inputs/empty.json",
},
},
args: []string{"-w", "my_workflow"},
expected: []string{
Args: []string{"-w", "my_workflow"},
Expected: []string{
"Interactive session for workflow my_workflow was successfully closed",
},
},
"error": {
serverResponses: map[string]ServerResponse{
ServerResponses: map[string]ServerResponse{
fmt.Sprintf(closePathTemplate, "my_workflow"): {
statusCode: http.StatusNotFound,
responseFile: "close_no_open.json",
StatusCode: http.StatusNotFound,
ResponseFile: "testdata/no_open.json",
},
},
args: []string{"-w", "my_workflow"},
expected: []string{"Workflow - my_workflow has no open interactive session."},
wantError: true,
Args: []string{"-w", "my_workflow"},
Expected: []string{"Workflow - my_workflow has no open interactive session."},
WantError: true,
},
}

for name, params := range tests {
t.Run(name, func(t *testing.T) {
params.cmd = "close"
testCmdRun(t, params)
params.Cmd = NewCmd()
TestCmdRun(t, params)
})
}
}
File renamed without changes.
17 changes: 9 additions & 8 deletions cmd/delete.go → cmd/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ REANA is free software; you can redistribute it and/or modify it
under the terms of the MIT License; see LICENSE file for more details.
*/

package cmd
// Package delete provides the command to delete a workflow.
package delete

import (
"fmt"
Expand All @@ -16,7 +17,7 @@ import (
"github.com/spf13/cobra"
)

const deleteDesc = `
const description = `
Delete a workflow.
The ` + "``delete``" + ` command removes workflow run(s) from the database. Note that
Expand All @@ -31,21 +32,21 @@ $ reana-client delete -w myanalysis.42
$ reana-client delete -w myanalysis.42 --include-all-runs
`

type deleteOptions struct {
type options struct {
token string
workflow string
includeWorkspace bool
includeAllRuns bool
}

// newDeleteCmd creates a command to delete a workflow.
func newDeleteCmd() *cobra.Command {
o := &deleteOptions{}
// NewCmd creates a command to delete a workflow.
func NewCmd() *cobra.Command {
o := &options{}

cmd := &cobra.Command{
Use: "delete",
Short: "Delete a workflow.",
Long: deleteDesc,
Long: description,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(cmd)
Expand All @@ -72,7 +73,7 @@ func newDeleteCmd() *cobra.Command {
return cmd
}

func (o *deleteOptions) run(cmd *cobra.Command) error {
func (o *options) run(cmd *cobra.Command) error {
err := workflows.UpdateStatus(
o.token,
o.workflow,
Expand Down
47 changes: 24 additions & 23 deletions cmd/delete_test.go → cmd/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ REANA is free software; you can redistribute it and/or modify it
under the terms of the MIT License; see LICENSE file for more details.
*/

package cmd
package delete

import (
"fmt"
"net/http"
. "reanahub/reana-client-go/cmd/internal"
"testing"
)

Expand All @@ -21,59 +22,59 @@ func TestDelete(t *testing.T) {

tests := map[string]TestCmdParams{
"default": {
serverResponses: map[string]ServerResponse{
ServerResponses: map[string]ServerResponse{
fmt.Sprintf(deletePathTemplate, workflowName): {
statusCode: http.StatusOK,
responseFile: "delete.json",
StatusCode: http.StatusOK,
ResponseFile: "testdata/success.json",
},
},
args: []string{"-w", workflowName},
expected: []string{
Args: []string{"-w", workflowName},
Expected: []string{
"my_workflow has been deleted",
},
},
"include workspace": {
serverResponses: map[string]ServerResponse{
ServerResponses: map[string]ServerResponse{
fmt.Sprintf(deletePathTemplate, workflowName): {
statusCode: http.StatusOK,
responseFile: "delete.json",
StatusCode: http.StatusOK,
ResponseFile: "testdata/success.json",
},
},
args: []string{"-w", workflowName, "--include-workspace"},
expected: []string{
Args: []string{"-w", workflowName, "--include-workspace"},
Expected: []string{
"my_workflow has been deleted",
},
},
"include all runs": {
serverResponses: map[string]ServerResponse{
ServerResponses: map[string]ServerResponse{
fmt.Sprintf(deletePathTemplate, workflowName): {
statusCode: http.StatusOK,
responseFile: "delete.json",
StatusCode: http.StatusOK,
ResponseFile: "testdata/success.json",
},
},
args: []string{"-w", workflowName, "--include-all-runs"},
expected: []string{
Args: []string{"-w", workflowName, "--include-all-runs"},
Expected: []string{
"All workflows named 'my_workflow' have been deleted",
},
},
"include all runs complete name": {
serverResponses: map[string]ServerResponse{
ServerResponses: map[string]ServerResponse{
fmt.Sprintf(deletePathTemplate, "my_workflow.10"): {
statusCode: http.StatusOK,
responseFile: "delete.json",
StatusCode: http.StatusOK,
ResponseFile: "testdata/success.json",
},
},
args: []string{"-w", "my_workflow.10", "--include-all-runs"},
expected: []string{
Args: []string{"-w", "my_workflow.10", "--include-all-runs"},
Expected: []string{
"All workflows named 'my_workflow' have been deleted",
},
},
}

for name, params := range tests {
t.Run(name, func(t *testing.T) {
params.cmd = "delete"
testCmdRun(t, params)
params.Cmd = NewCmd()
TestCmdRun(t, params)
})
}
}
File renamed without changes.
23 changes: 13 additions & 10 deletions cmd/diff.go → cmd/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ REANA is free software; you can redistribute it and/or modify it
under the terms of the MIT License; see LICENSE file for more details.
*/

package cmd
// Package diff provides the command to show diff between two workflows.
package diff

import (
"encoding/json"
Expand All @@ -25,7 +26,7 @@ import (
"github.com/spf13/cobra"
)

const diffDesc = `
const description = `
Show diff between two workflows.
The ` + "``diff``" + ` command allows to compare two workflows, the workflow_a and
Expand All @@ -39,22 +40,22 @@ Examples:
$ reana-client diff myanalysis.42 myotheranalysis.43 --brief
`

type diffOptions struct {
type options struct {
token string
workflowA string
workflowB string
brief bool
unified int
}

// newDiffCmd creates a command to show diff between two workflows.
func newDiffCmd() *cobra.Command {
o := &diffOptions{}
// NewCmd creates a command to show diff between two workflows.
func NewCmd() *cobra.Command {
o := &options{}

cmd := &cobra.Command{
Use: "diff",
Short: "Show diff between two workflows.",
Long: diffDesc,
Long: description,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
o.workflowA = args[0]
Expand All @@ -74,7 +75,7 @@ files in the two workspaces are shown.`)
return cmd
}

func (o *diffOptions) run(cmd *cobra.Command) error {
func (o *options) run(cmd *cobra.Command) error {
diffParams := operations.NewGetWorkflowDiffParams()
diffParams.SetAccessToken(&o.token)
diffParams.SetWorkflowIDOrNamea(o.workflowA)
Expand All @@ -92,15 +93,16 @@ func (o *diffOptions) run(cmd *cobra.Command) error {
return err
}

err = displayDiffPayload(cmd, diffResp.Payload)
err = displayPayload(cmd, diffResp.Payload)
if err != nil {
return err
}

return nil
}

func displayDiffPayload(cmd *cobra.Command, p *operations.GetWorkflowDiffOKBody) error {
// displayPayload displays the diff payload.
func displayPayload(cmd *cobra.Command, p *operations.GetWorkflowDiffOKBody) error {
if p.ReanaSpecification != "" {
specificationDiff := orderedmap.New()
err := json.Unmarshal([]byte(p.ReanaSpecification), &specificationDiff)
Expand Down Expand Up @@ -173,6 +175,7 @@ func displayDiffPayload(cmd *cobra.Command, p *operations.GetWorkflowDiffOKBody)
return nil
}

// printDiff prints the diff contained int the given lines.
func printDiff(lines []string, out io.Writer) {
for _, line := range lines {
lineColor := text.Reset
Expand Down
Loading

0 comments on commit 6c1147e

Please sign in to comment.