Repository: exercism/cli
Pull request #617 (2018-07-13 11:43:14)

Get rid of transmission type
This inlines the little bit of logic that we needed from the workspace.Transition
and deletes the type.

We may find that we want to re-introduce an abstraction for some of this,
but I want to write it all down as simply as possible first and then go from there.

Closes https://github.com/exercism/cli/issues/613


Patch:
diff --git a/cmd/submit.go b/cmd/submit.go
index a108b28..6b851bb 100644
--- a/cmd/submit.go
+++ b/cmd/submit.go
@@ -98,6 +98,12 @@ func runSubmit(cfg config.Configuration, flags *pflag.FlagSet, args []string) er
 	}
 
 	for i, arg := range args {
+		var err error
+		arg, err = filepath.Abs(arg)
+		if err != nil {
+			return err
+		}
+
 		info, err := os.Lstat(arg)
 		if err != nil {
 			if os.IsNotExist(err) {
@@ -135,12 +141,25 @@ func runSubmit(cfg config.Configuration, flags *pflag.FlagSet, args []string) er
 		return err
 	}
 
-	tx, err := workspace.NewTransmission(ws.Dir, args)
-	if err != nil {
-		return err
+	var exerciseDir string
+	for _, arg := range args {
+		dir, err := ws.SolutionDir(arg)
+		if err != nil {
+			return err
+		}
+		if exerciseDir != "" && dir != exerciseDir {
+			msg := `
+
+    You are submitting files belonging to different solutions.
+    Please submit the files for one solution at a time.
+
+		`
+			return errors.New(msg)
+		}
+		exerciseDir = dir
 	}
 
-	dirs, err := ws.Locate(tx.Dir)
+	dirs, err := ws.Locate(exerciseDir)
 	if err != nil {
 		return err
 	}
@@ -183,8 +202,8 @@ func runSubmit(cfg config.Configuration, flags *pflag.FlagSet, args []string) er
 		return fmt.Errorf(msg, BinaryName, solution.Exercise, solution.Track)
 	}
 
-	paths := make([]string, 0, len(tx.Files))
-	for _, file := range tx.Files {
+	paths := make([]string, 0, len(args))
+	for _, file := range args {
 		// Don't submit empty files
 		info, err := os.Stat(file)
 		if err != nil {
diff --git a/workspace/transmission.go b/workspace/transmission.go
deleted file mode 100644
index 7ea77fa..0000000
--- a/workspace/transmission.go
+++ /dev/null
@@ -1,62 +0,0 @@
-package workspace
-
-import (
-	"errors"
-	"path/filepath"
-)
-
-// Transmission is the data necessary to submit a solution.
-type Transmission struct {
-	Files   []string
-	Dir     string
-	ArgDirs []string
-}
-
-// NewTransmission processes the arguments to the submit command to prep a submission.
-func NewTransmission(root string, args []string) (*Transmission, error) {
-	tx := &Transmission{}
-	for _, arg := range args {
-		pt, err := DetectPathType(arg)
-		if err != nil {
-			return nil, err
-		}
-		if pt == TypeFile {
-			arg, err = filepath.Abs(arg)
-			if err != nil {
-				return nil, err
-			}
-			tx.Files = append(tx.Files, arg)
-			continue
-		}
-		// For our purposes, if it's not a file then it's a directory.
-		tx.ArgDirs = append(tx.ArgDirs, arg)
-	}
-	if len(tx.ArgDirs) > 1 {
-		return nil, errors.New("more than one dir")
-	}
-	if len(tx.ArgDirs) > 0 && len(tx.Files) > 0 {
-		return nil, errors.New("mixing files and dirs")
-	}
-	if len(tx.Files) > 0 {
-		ws, err := New(root)
-		if err != nil {
-			return nil, err
-		}
-		parents := map[string]bool{}
-		for _, file := range tx.Files {
-			dir, err := ws.SolutionDir(file)
-			if err != nil {
-				return nil, err
-			}
-			parents[dir] = true
-			tx.Dir = dir
-		}
-		if len(parents) > 1 {
-			return nil, errors.New("files are from more than one solution")
-		}
-	}
-	if len(tx.ArgDirs) == 1 {
-		tx.Dir = tx.ArgDirs[0]
-	}
-	return tx, nil
-}


Test patch:
diff --git a/cmd/submit_test.go b/cmd/submit_test.go
index ab35eae..3b96506 100644
--- a/cmd/submit_test.go
+++ b/cmd/submit_test.go
@@ -266,7 +266,7 @@ func TestSubmitFilesFromDifferentSolutions(t *testing.T) {
 
 	err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{file1, file2})
 	assert.Error(t, err)
-	assert.Regexp(t, "more than one solution", err.Error())
+	assert.Regexp(t, "different solutions", err.Error())
 }
 
 func fakeSubmitServer(t *testing.T, submittedFiles map[string]string) *httptest.Server {
diff --git a/workspace/transmission_test.go b/workspace/transmission_test.go
deleted file mode 100644
index f379e6f..0000000
--- a/workspace/transmission_test.go
+++ /dev/null
@@ -1,100 +0,0 @@
-// +build !windows
-
-package workspace
-
-import (
-	"io/ioutil"
-	"os"
-	"path/filepath"
-	"runtime"
-	"testing"
-
-	"github.com/stretchr/testify/assert"
-)
-
-func TestNewTransmission(t *testing.T) {
-	_, cwd, _, _ := runtime.Caller(0)
-	root := filepath.Join(cwd, "..", "..", "fixtures", "transmission")
-	dirBird := filepath.Join(root, "creatures", "hummingbird")
-	dirFeeder := filepath.Join(dirBird, "feeder")
-	fileBird := filepath.Join(dirBird, "hummingbird.txt")
-	fileSugar := filepath.Join(dirFeeder, "sugar.txt")
-
-	testCases := []struct {
-		desc string
-		args []string
-		ok   bool
-		tx   *Transmission
-	}{
-		{
-			desc: "more than one dir",
-			args: []string{dirBird, dirFeeder},
-			ok:   false,
-		},
-		{
-			desc: "a file and a dir",
-			args: []string{dirBird, fileBird},
-			ok:   false,
-		},
-		{
-			desc: "just one file",
-			args: []string{fileBird},
-			ok:   true,
-			tx:   &Transmission{Files: []string{fileBird}, Dir: dirBird},
-		},
-		{
-			desc: "multiple files",
-			args: []string{fileBird, fileSugar},
-			ok:   true,
-			tx:   &Transmission{Files: []string{fileBird, fileSugar}, Dir: dirBird},
-		},
-		{
-			desc: "one dir",
-			args: []string{dirBird},
-			ok:   true,
-			tx:   &Transmission{Files: nil, Dir: dirBird},
-		},
-		{
-			desc: "multiple exercise names",
-			args: []string{"hummingbird", "bear"},
-			ok:   false,
-		},
-		{
-			desc: "one exercise name",
-			args: []string{"hummingbird"},
-			ok:   true,
-			tx:   &Transmission{Files: nil, Dir: "hummingbird"},
-		},
-	}
-
-	for _, tc := range testCases {
-		t.Run(tc.desc, func(t *testing.T) {
-			tx, err := NewTransmission(root, tc.args)
-			if tc.ok {
-				assert.NoError(t, err, tc.desc)
-			} else {
-				assert.Error(t, err, tc.desc)
-			}
-
-			if tc.tx != nil {
-				assert.Equal(t, tc.tx.Files, tx.Files)
-				assert.Equal(t, tc.tx.Dir, tx.Dir)
-			}
-		})
-	}
-}
-
-func TestTransmissionWithRelativePath(t *testing.T) {
-	// This is really dirty, but I need to make sure that we turn relative paths into absolute paths.
-	err := ioutil.WriteFile(".solution.json", []byte("{}"), os.FileMode(0755))
-	assert.NoError(t, err)
-	defer os.Remove(".solution.json")
-
-	_, cwd, _, _ := runtime.Caller(0)
-	dir := filepath.Dir(filepath.Dir(cwd))
-	file := filepath.Base(cwd)
-	tx, err := NewTransmission(dir, []string{file})
-	if assert.NoError(t, err) {
-		assert.Equal(t, filepath.Clean(cwd), tx.Files[0])
-	}
-}
diff --git a/workspace/transmission_windows_test.go b/workspace/transmission_windows_test.go
deleted file mode 100644
index 91dabc8..0000000
--- a/workspace/transmission_windows_test.go
+++ /dev/null
@@ -1,83 +0,0 @@
-package workspace
-
-import (
-	"path/filepath"
-	"runtime"
-	"testing"
-
-	"github.com/stretchr/testify/assert"
-)
-
-func TestNewTransmission(t *testing.T) {
-	t.Skip("This panics on Windows. Once debugged, this can likely be inlined back into the main transmission test.")
-
-	_, cwd, _, _ := runtime.Caller(0)
-	root := filepath.Join(cwd, "..", "..", "fixtures", "transmission")
-	dirBird := filepath.Join(root, "creatures", "hummingbird")
-	dirFeeder := filepath.Join(dirBird, "feeder")
-	fileBird := filepath.Join(dirBird, "hummingbird.txt")
-	fileSugar := filepath.Join(dirFeeder, "sugar.txt")
-
-	testCases := []struct {
-		desc string
-		args []string
-		ok   bool
-		tx   *Transmission
-	}{
-		{
-			desc: "more than one dir",
-			args: []string{dirBird, dirFeeder},
-			ok:   false,
-		},
-		{
-			desc: "a file and a dir",
-			args: []string{dirBird, fileBird},
-			ok:   false,
-		},
-		{
-			desc: "just one file",
-			args: []string{fileBird},
-			ok:   true,
-			tx:   &Transmission{Files: []string{fileBird}, Dir: dirBird},
-		},
-		{
-			desc: "multiple files",
-			args: []string{fileBird, fileSugar},
-			ok:   true,
-			tx:   &Transmission{Files: []string{fileBird, fileSugar}, Dir: dirBird},
-		},
-		{
-			desc: "one dir",
-			args: []string{dirBird},
-			ok:   true,
-			tx:   &Transmission{Files: nil, Dir: dirBird},
-		},
-		{
-			desc: "multiple exercise names",
-			args: []string{"hummingbird", "bear"},
-			ok:   false,
-		},
-		{
-			desc: "one exercise name",
-			args: []string{"hummingbird"},
-			ok:   true,
-			tx:   &Transmission{Files: nil, Dir: "hummingbird"},
-		},
-	}
-
-	for _, tc := range testCases {
-		t.Run(tc.desc, func(t *testing.T) {
-			tx, err := NewTransmission(root, tc.args)
-			if tc.ok {
-				assert.NoError(t, err, tc.desc)
-			} else {
-				assert.Error(t, err, tc.desc)
-			}
-
-			if tc.tx != nil {
-				assert.Equal(t, tc.tx.Files, tx.Files)
-				assert.Equal(t, tc.tx.Dir, tx.Dir)
-			}
-		})
-	}
-}