Skip to content

Commit

Permalink
fix(CreateDataset): user-specified data should override transforms
Browse files Browse the repository at this point in the history
ugh I screwed up my commits & don't have time to de-couple them. This commit also refactors our tests in the cmd package to make them more readable / sensible

closes #456
  • Loading branch information
b5 committed Jun 12, 2018
1 parent d2dc519 commit 536e67b
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 170 deletions.
72 changes: 34 additions & 38 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func confirmQriNotRunning() error {
return nil
}

func executeCommand(root *cobra.Command, args ...string) (output string, err error) {
func executeCommand(root *cobra.Command, cmd string) (output string, err error) {
cmd = strings.TrimPrefix(cmd, "qri ")
// WARNING - currently doesn't support quoted strings as input
args := strings.Split(cmd, " ")
_, output, err = executeCommandC(root, args...)
return output, err
}
Expand Down Expand Up @@ -102,11 +105,6 @@ func TestCommandsIntegration(t *testing.T) {

path := filepath.Join(os.TempDir(), "qri_test_commands_integration")
t.Logf("test filepath: %s", path)
fmt.Println(path)

// fmt.Printf("temp path: %s", path)
os.Setenv("IPFS_PATH", filepath.Join(path, "ipfs"))
os.Setenv("QRI_PATH", filepath.Join(path, "qri"))

//clean up if previous cleanup failed
if _, err := os.Stat(path); os.IsNotExist(err) {
Expand Down Expand Up @@ -142,50 +140,48 @@ func TestCommandsIntegration(t *testing.T) {
return
}

commands := [][]string{
{"help"},
{"version"},
{"setup", "--peername=" + "alan", "--registry=" + registryServer.URL},
{"config", "get", "-c"},
{"config", "get", "profile"},
{"config", "set", "webapp.port", "3505"},
// TODO - add setting whole config via a file
// {"config", "set", "-i" + profileDataFilepath},
{"info", "me"},
{"add", "--data=" + moviesFilePath, "me/movies"},
{"add", "--data=" + movies2FilePath, "me/movies2"},
{"add", "--data=" + linksFilepath, "me/links"},
{"info", "me/movies"},
{"list"},
{"save", "--data=" + movies2FilePath, "-t" + "commit_1", "me/movies"},
{"log", "me/movies"},
{"diff", "me/movies", "me/movies2", "-d", "detail"},
{"export", "-o" + path, "me/movies"},
{"export", "-o" + path, "--format=cbor", "--body-format=json", "me/movies"},
{"registry", "unpublish", "me/movies"},
{"registry", "publish", "me/movies"},
{"rename", "me/movies", "me/movie"},
{"data", "--limit=1", "--data-format=cbor", "me/movie"},
{"validate", "me/movie"},
{"remove", "me/movie"},
{"export", "--blank", "-o" + path + "/blank_dataset.yaml"},
{"setup", "--remove"},
commands := []string{
"qri help",
"qri version",
fmt.Sprintf("qri setup --peername=alan --registry=%s", registryServer.URL),
"qri config get -c",
"qri config get profile",
"qri config set webapp.port 3505",
"qri info me",
fmt.Sprintf("qri add --data=%s me/movies", moviesFilePath),
fmt.Sprintf("qri add --data=%s me/movies2", movies2FilePath),
fmt.Sprintf("qri add --data=%s me/links", linksFilepath),
"qri info me/movies",
"qri list",
fmt.Sprintf("qri save --data=%s -t=commit_1 me/movies", movies2FilePath),
"qri log me/movies",
"qri diff me/movies me/movies2 -d=detail",
fmt.Sprintf("qri export -o=%s me/movies",path),
fmt.Sprintf("qri export -o=%s --format=cbor --body-format=json me/movies", path),
"qri registry unpublish me/movies",
"qri registry publish me/movies",
"qri rename me/movies me/movie",
"qri data --limit=1 --data-format=cbor me/movie",
"qri validate me/movie",
"qri remove me/movie",
fmt.Sprintf("qri export --blank -o=%s/blank_dataset.yaml", path),
"qri setup --remove",
}

_, in, out, err := NewTestIOStreams()
root := NewQriCommand(NewDirPathFactory(path), in, out, err)

for i, args := range commands {
for i, command := range commands {
func() {
defer func() {
if e := recover(); e != nil {
t.Errorf("case %d unexpected panic executing command\n%s\n%s", i, strings.Join(args, " "), e)
t.Errorf("case %d unexpected panic executing command\n%s\n%s", i, command, e)
return
}
}()
_, err := executeCommand(root, args...)
_, err := executeCommand(root, command)
if err != nil {
t.Errorf("case %d unexpected error executing command\n%s\n%s", i, strings.Join(args, " "), err.Error())
t.Errorf("case %d unexpected error executing command\n%s\n%s", i, command, err.Error())
return
}
}()
Expand Down
21 changes: 10 additions & 11 deletions cmd/connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"os"
"path/filepath"
"strings"
"testing"

regmock "github.com/qri-io/registry/regserver/mock"
Expand All @@ -30,20 +29,20 @@ func TestConnect(t *testing.T) {
}
defer os.RemoveAll(path)

// defer func() {
// if e := recover(); e != nil {
// t.Errorf("unexpected panic:\n%s\n%s", strings.Join(args, " "), e)
// return
// }
// }()

args := []string{"connect", "--setup", "--registry=" + registryServer.URL, "--disconnect-after=3"}
cmd := "qri connect --setup --registry=" + registryServer.URL + " --disconnect-after=1"
_, in, out, errs := NewTestIOStreams()
root := NewQriCommand(NewDirPathFactory(path), in, out, errs)

_, err := executeCommand(root, args...)
defer func() {
if e := recover(); e != nil {
t.Errorf("unexpected panic:\n%s\n%s", cmd, e)
return
}
}()

_, err := executeCommand(root, cmd)
if err != nil {
t.Errorf("unexpected error executing command\n%s\n%s", strings.Join(args, " "), err.Error())
t.Errorf("unexpected error executing command\n%s\n%s", cmd, err.Error())
return
}
}
5 changes: 2 additions & 3 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/qri-io/dataset"
"github.com/qri-io/qri/config"
"github.com/qri-io/qri/core"
"github.com/qri-io/qri/repo"
"github.com/spf13/cobra"
)
Expand All @@ -35,8 +34,8 @@ const (
// var printPrompt = color.New(color.FgWhite).PrintfFunc()
var spinner = sp.New(sp.CharSets[24], 100*time.Millisecond)

func setNoColor() {
color.NoColor = core.Config.CLI.ColorizeOutput
func setNoColor(noColor bool) {
color.NoColor = noColor
}

func printSuccess(w io.Writer, msg string, params ...interface{}) {
Expand Down
8 changes: 6 additions & 2 deletions cmd/qri.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ func NewQriCommand(pf PathFactory, in io.Reader, out, err io.Writer) *cobra.Comm

// TODO: write a test that verifies this works with our new yaml config
// RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $QRI_PATH/config.yaml)")
// RootCmd.PersistentFlags().BoolVarP(&noColor, "no-color", "c", false, "disable colorized output")
cmd.SetUsageTemplate(rootUsageTemplate)
cmd.PersistentFlags().BoolVarP(&opt.NoPrompt, "no-prompt", "", false, "disable all interactive prompts")
cmd.Flags().BoolVarP(&opt.NoPrompt, "no-prompt", "", false, "disable all interactive prompts")
cmd.Flags().BoolVarP(&opt.NoColor, "no-color", "", false, "disable colorized output")

cmd.AddCommand(
NewAddCommand(opt, ioStreams),
NewConfigCommand(opt, ioStreams),
Expand Down Expand Up @@ -82,6 +83,8 @@ type QriOptions struct {
ipfsFsPath string
// NoPrompt Disables all promt messages
NoPrompt bool
// NoColor disables colorized output
NoColor bool
// path to configuration object
ConfigPath string

Expand Down Expand Up @@ -113,6 +116,7 @@ func (o *QriOptions) init() (err error) {
return
}
o.config = core.Config
setNoColor(o.config.CLI.ColorizeOutput || o.NoColor)

addr := fmt.Sprintf(":%d", o.config.RPC.Port)
if conn, err := net.Dial("tcp", addr); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ overwrite this info.`,
cmd.Flags().StringVarP(&o.Registry, "registry", "", "", "override default registry URL")
cmd.Flags().StringVarP(&o.Peername, "peername", "", "", "choose your desired peername")
cmd.Flags().StringVarP(&o.IPFSConfigData, "ipfs-config", "", "", "json-encoded configuration data, specify a filepath with '@' prefix")
cmd.Flags().StringVarP(&o.ConfigData, "conifg-data", "", "", "json-encoded configuration data, specify a filepath with '@' prefix")
cmd.Flags().StringVarP(&o.ConfigData, "config-data", "", "", "json-encoded configuration data, specify a filepath with '@' prefix")

return cmd
}
Expand Down
109 changes: 1 addition & 108 deletions cmd/tutorials_test.go
Original file line number Diff line number Diff line change
@@ -1,117 +1,10 @@
package cmd

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

regmock "github.com/qri-io/registry/regserver/mock"
)

func TestTutorialSkylarkTransformSecOne(t *testing.T) {
testCommandSection(t, []subSection{
subSection{
name: "1.0",
files: map[string]string{
"dataset.yaml": `name: hello_world
meta:
title: hello world example
transform:
scriptpath: $PATH/transform.sky
`,
"transform.sky": `
def transform(qri):
return(["hello", "world"])
`,
},
commands: map[string]string{
"qri add --file=$PATH/dataset.yaml": "",
"qri info me/hello_world": "",
"qri data me/hello_world": "",
"qri data -s 1 me/hello_world": "",
},
},

subSection{
name: "1.1",
files: map[string]string{
"transform.sky": `
def transform(qri):
qri.set_meta("description", "this is an example dataset to learn about transformations")
return(["hello","world"])
`,
},
commands: map[string]string{
"qri update --file=$PATH/dataset.yaml": "",
"qri info me/hello_world": "",
"qri log me/hello_world": "",
},
},
})
}

type subSection struct {
name string
commands, files map[string]string
}

func testCommandSection(t *testing.T, subSections []subSection) {
t.Skip("not yet finished")

if err := confirmQriNotRunning(); err != nil {
t.Skip(err.Error())
}

_, registryServer := regmock.NewMockServer()

path := filepath.Join(os.TempDir(), "qri_test_section")
t.Logf("test filepath: %s", path)
// fmt.Println(path)

//clean up if previous cleanup failed
if _, err := os.Stat(path); os.IsNotExist(err) {
os.RemoveAll(path)
}
if err := os.MkdirAll(path, os.ModePerm); err != nil {
t.Errorf("error creating test path: %s", err.Error())
return
}
defer os.RemoveAll(path)

// fmt.Printf("temp path: %s", path)
os.Setenv("IPFS_PATH", filepath.Join(path, "ipfs"))
os.Setenv("QRI_PATH", filepath.Join(path, "qri"))

_, in, out, errs := NewTestIOStreams()
root := NewQriCommand(NewDirPathFactory(path), in, out, errs)

// run setup
setup := fmt.Sprintf("setup --peername=alan --registry=%s", registryServer.URL)
if _, err := executeCommand(root, strings.Split(setup, " ")...); err != nil {
t.Fatal(err.Error())
}

// initializeCLI()
// loadConfig()

for _, ss := range subSections {
for name, data := range ss.files {
data = strings.Replace(data, "$PATH", path, -1)
ioutil.WriteFile(filepath.Join(path, name), []byte(data), os.ModePerm)
}

for cmd := range ss.commands {
cmd = strings.Replace(cmd, "$PATH", path, -1)
cmd = strings.TrimPrefix(cmd, "qri ")
_, err := executeCommand(root, strings.Split(cmd, " ")...)
if err != nil {
t.Fatal(err.Error())
}
}

}

}

Loading

0 comments on commit 536e67b

Please sign in to comment.