11package cmd
22
33import (
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,34 @@ func TestSubmitOnlyEmptyFile(t *testing.T) {
234236 assert .Regexp (t , "No files found" , err .Error ())
235237}
236238
239+ func TestSubmitExerciseWithLegacySolutionMetadataFile (t * testing.T ) {
240+ tmpDir , err := ioutil .TempDir ("" , "legacy-metadata-file" )
241+ assert .NoError (t , err )
242+
243+ dir := filepath .Join (tmpDir , "bogus-track" , "bogus-exercise" )
244+ os .MkdirAll (dir , os .FileMode (0755 ))
245+ writeFakeLegacySolution (t , dir , "bogus-track" , "bogus-exercise" )
246+
247+ file := filepath .Join (dir , "file.txt" )
248+ err = ioutil .WriteFile (file , []byte ("This is a file." ), os .FileMode (0755 ))
249+ assert .NoError (t , err )
250+
251+ v := viper .New ()
252+ v .Set ("token" , "abc123" )
253+ v .Set ("workspace" , tmpDir )
254+ cfg := config.Configuration {
255+ Persister : config.InMemoryPersister {},
256+ Dir : tmpDir ,
257+ UserViperConfig : v ,
258+ }
259+ solutionRelPath := filepath .Join (workspace .IgnoreSubdir , workspace .SolutionFilename )
260+ expectedSolutionPathAfterMigration := filepath .Join (dir , solutionRelPath )
261+
262+ _ = runSubmit (cfg , pflag .NewFlagSet ("fake" , pflag .PanicOnError ), []string {file })
263+ _ , err = os .Stat (expectedSolutionPathAfterMigration )
264+ assert .NoError (t , err )
265+ }
266+
237267func TestSubmitFilesFromDifferentSolutions (t * testing.T ) {
238268 tmpDir , err := ioutil .TempDir ("" , "dir-1-submit" )
239269 assert .NoError (t , err )
@@ -305,3 +335,25 @@ func writeFakeSolution(t *testing.T, dir, trackID, exerciseSlug string) {
305335 err := solution .Write (dir )
306336 assert .NoError (t , err )
307337}
338+
339+ func writeFakeLegacySolution (t * testing.T , dir , trackID , exerciseSlug string ) {
340+ solution := & workspace.Solution {
341+ ID : "bogus-solution-uuid" ,
342+ Track : trackID ,
343+ Exercise : exerciseSlug ,
344+ URL : "http://example.com/bogus-url" ,
345+ IsRequester : true ,
346+ }
347+ const legacySolutionName = ".solution.json"
348+
349+ b , err := json .Marshal (solution )
350+ path := filepath .Join (dir , legacySolutionName )
351+
352+ // Hack because ioutil.WriteFile fails on hidden files
353+ visibility .ShowFile (path )
354+
355+ err = ioutil .WriteFile (path , b , os .FileMode (0600 ))
356+ solution .Dir = dir
357+ err = visibility .HideFile (path )
358+ assert .NoError (t , err )
359+ }
0 commit comments