Skip to content

Commit 34c4ec6

Browse files
committed
Add test for submitting with legacy solution
1 parent 60b57f1 commit 34c4ec6

3 files changed

Lines changed: 78 additions & 11 deletions

File tree

cmd/submit_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"io/ioutil"
56
"net/http"
67
"net/http/httptest"
@@ -9,6 +10,7 @@ import (
910
"testing"
1011

1112
"github.com/exercism/cli/config"
13+
"github.com/exercism/cli/visibility"
1214
"github.com/exercism/cli/workspace"
1315
"github.com/spf13/pflag"
1416
"github.com/spf13/viper"
@@ -234,6 +236,51 @@ func TestSubmitOnlyEmptyFile(t *testing.T) {
234236
assert.Regexp(t, "No files found", err.Error())
235237
}
236238

239+
func TestSubmitExerciseWithLegacySolutionMetadataFileAndGetsMigrated(t *testing.T) {
240+
oldOut := Out
241+
oldErr := Err
242+
Out = ioutil.Discard
243+
Err = ioutil.Discard
244+
defer func() {
245+
Out = oldOut
246+
Err = oldErr
247+
}()
248+
// The fake endpoint will populate this when it receives the call from the command.
249+
submittedFiles := map[string]string{}
250+
ts := fakeSubmitServer(t, submittedFiles)
251+
defer ts.Close()
252+
253+
tmpDir, err := ioutil.TempDir("", "legacy-metadata-file")
254+
assert.NoError(t, err)
255+
256+
dir := filepath.Join(tmpDir, "bogus-track", "bogus-exercise")
257+
os.MkdirAll(dir, os.FileMode(0755))
258+
writeFakeLegacySolution(t, dir, "bogus-track", "bogus-exercise")
259+
260+
file := filepath.Join(dir, "file.txt")
261+
err = ioutil.WriteFile(file, []byte("This is a file."), os.FileMode(0755))
262+
assert.NoError(t, err)
263+
264+
v := viper.New()
265+
v.Set("token", "abc123")
266+
v.Set("workspace", tmpDir)
267+
v.Set("apibaseurl", ts.URL)
268+
cfg := config.Configuration{
269+
Persister: config.InMemoryPersister{},
270+
Dir: tmpDir,
271+
UserViperConfig: v,
272+
}
273+
solutionRelPath := filepath.Join(workspace.IgnoreSubdir, workspace.SolutionFilename)
274+
expectedSolutionPathAfterMigration := filepath.Join(dir, solutionRelPath)
275+
276+
err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{file})
277+
assert.NoError(t, err)
278+
assert.Equal(t, "This is a file.", submittedFiles[string(os.PathSeparator)+"file.txt"])
279+
280+
_, err = os.Stat(expectedSolutionPathAfterMigration)
281+
assert.NoError(t, err)
282+
}
283+
237284
func TestSubmitFilesFromDifferentSolutions(t *testing.T) {
238285
tmpDir, err := ioutil.TempDir("", "dir-1-submit")
239286
assert.NoError(t, err)
@@ -305,3 +352,26 @@ func writeFakeSolution(t *testing.T, dir, trackID, exerciseSlug string) {
305352
err := solution.Write(dir)
306353
assert.NoError(t, err)
307354
}
355+
356+
func writeFakeLegacySolution(t *testing.T, dir, trackID, exerciseSlug string) {
357+
solution := &workspace.Solution{
358+
ID: "bogus-solution-uuid",
359+
Track: trackID,
360+
Exercise: exerciseSlug,
361+
URL: "http://example.com/bogus-url",
362+
IsRequester: true,
363+
}
364+
legacySolutionFilePath := filepath.Join(dir, ".solution.json")
365+
366+
// Hack because ioutil.WriteFile fails on hidden files
367+
visibility.ShowFile(legacySolutionFilePath)
368+
369+
b, err := json.Marshal(solution)
370+
assert.NoError(t, err)
371+
372+
err = ioutil.WriteFile(legacySolutionFilePath, b, os.FileMode(0600))
373+
assert.NoError(t, err)
374+
375+
err = visibility.HideFile(legacySolutionFilePath)
376+
assert.NoError(t, err)
377+
}

workspace/solution.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
"github.com/exercism/cli/visibility"
1414
)
1515

16-
const ignoreSubdir = ".exercism"
17-
const solutionFilename = "solution.json"
16+
const IgnoreSubdir = ".exercism"
17+
const SolutionFilename = "solution.json"
1818

19-
var solutionRelPath = filepath.Join(ignoreSubdir, solutionFilename)
19+
var solutionRelPath = filepath.Join(IgnoreSubdir, SolutionFilename)
2020

2121
// Solution contains metadata about a user's solution.
2222
type Solution struct {
@@ -71,7 +71,7 @@ func (s *Solution) Write(dir string) error {
7171
return err
7272
}
7373

74-
path := filepath.Join(dir, ignoreSubdir)
74+
path := filepath.Join(dir, IgnoreSubdir)
7575
if err := createIgnoreSubdir(path); err != nil {
7676
return err
7777
}
@@ -106,7 +106,7 @@ func createIgnoreSubdir(path string) error {
106106
}
107107

108108
func migrateLegacySolutionFile(path string, legacySolutionPath string, solutionPath string) error {
109-
if err := createIgnoreSubdir(filepath.Join(path, ignoreSubdir)); err != nil {
109+
if err := createIgnoreSubdir(filepath.Join(path, IgnoreSubdir)); err != nil {
110110
return err
111111
}
112112
if err := os.Rename(legacySolutionPath, solutionPath); err != nil {

workspace/workspace.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (ws Workspace) SolutionDir(s string) (string, error) {
186186
path := s
187187
for {
188188
if path == ws.Dir {
189-
return "", fmt.Errorf("couldn't find %s", solutionFilename)
189+
return "", fmt.Errorf("couldn't find %s", SolutionFilename)
190190
}
191191
if _, err := os.Lstat(path); os.IsNotExist(err) {
192192
return "", err
@@ -202,13 +202,10 @@ func checkSolutionFile(path string) error {
202202
legacySolutionPath := filepath.Join(path, ".solution.json")
203203
solutionPath := filepath.Join(path, solutionRelPath)
204204

205-
if _, legacyErr := os.Lstat(legacySolutionPath); legacyErr == nil {
205+
if _, err := os.Lstat(legacySolutionPath); err == nil {
206206
return migrateLegacySolutionFile(path, legacySolutionPath, solutionPath)
207207
} else if _, err := os.Lstat(solutionPath); err != nil {
208208
return err
209-
} else if legacyErr != nil && err == nil {
210-
return nil
211-
} else {
212-
return legacyErr
213209
}
210+
return nil
214211
}

0 commit comments

Comments
 (0)