Skip to content

Commit

Permalink
Refactor MigrateLegacyMetadataFile to separate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdsutherland committed Aug 28, 2018
1 parent 05545c5 commit 8849c19
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions workspace/exercise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,62 +44,85 @@ func TestNewFromDir(t *testing.T) {
assert.Equal(t, "the-exercise", exercise.Slug)
}

func TestMigrateLegacyMetadataFile(t *testing.T) {
var str string
func TestMigrateLegacyMetadataFileWithoutLegacy(t *testing.T) {
ws, err := ioutil.TempDir("", "fake-workspace")
defer os.RemoveAll(ws)
assert.NoError(t, err)

exercise := Exercise{Root: ws, Track: "bogus-track", Slug: "migration"}
exercise := Exercise{Root: ws, Track: "bogus-track", Slug: "no-legacy"}
metadataFilepath := exercise.MetadataFilepath()
legacyMetadataFilepath := exercise.LegacyMetadataFilepath()

err = os.MkdirAll(filepath.Dir(legacyMetadataFilepath), os.FileMode(0755))
assert.NoError(t, err)
err = os.MkdirAll(filepath.Dir(metadataFilepath), os.FileMode(0755))
assert.NoError(t, err)

// returns nil if not legacy
err = ioutil.WriteFile(metadataFilepath, []byte{}, os.FileMode(0600))
assert.NoError(t, err)
ok, _ := exercise.HasMetadata()
assert.True(t, ok)
_, err = exercise.MigrateLegacyMetadataFile()

stderr, err := exercise.MigrateLegacyMetadataFile()

assert.Nil(t, err)
assert.Equal(t, "", stderr)
ok, _ = exercise.HasMetadata()
assert.True(t, ok)
}

func TestMigrateLegacyMetadataFileWithLegacy(t *testing.T) {
ws, err := ioutil.TempDir("", "fake-workspace")
defer os.RemoveAll(ws)
assert.NoError(t, err)

exercise := Exercise{Root: ws, Track: "bogus-track", Slug: "legacy"}
metadataFilepath := exercise.MetadataFilepath()
legacyMetadataFilepath := exercise.LegacyMetadataFilepath()
err = os.MkdirAll(filepath.Dir(legacyMetadataFilepath), os.FileMode(0755))
assert.NoError(t, err)

// legacy metadata only => gets renamed
os.Remove(metadataFilepath)
err = ioutil.WriteFile(legacyMetadataFilepath, []byte{}, os.FileMode(0600))
assert.NoError(t, err)
ok, _ = exercise.HasLegacyMetadata()
ok, _ := exercise.HasLegacyMetadata()
assert.True(t, ok)
ok, _ = exercise.HasMetadata()
assert.False(t, ok)
str, err = exercise.MigrateLegacyMetadataFile()
assert.Equal(t, fmt.Sprintf("\nMigrated metadata to %s\n", metadataFilepath), str)

stderr, err := exercise.MigrateLegacyMetadataFile()

assert.Equal(t, fmt.Sprintf("\nMigrated metadata to %s\n", metadataFilepath), stderr)
assert.NoError(t, err)
ok, _ = exercise.HasLegacyMetadata()
assert.False(t, ok)
ok, _ = exercise.HasMetadata()
assert.True(t, ok)
}

func TestMigrateLegacyMetadataFileWithLegacyAndModern(t *testing.T) {
ws, err := ioutil.TempDir("", "fake-workspace")
defer os.RemoveAll(ws)
assert.NoError(t, err)

exercise := Exercise{Root: ws, Track: "bogus-track", Slug: "both-legacy-and-modern"}
metadataFilepath := exercise.MetadataFilepath()
legacyMetadataFilepath := exercise.LegacyMetadataFilepath()
err = os.MkdirAll(filepath.Dir(legacyMetadataFilepath), os.FileMode(0755))
assert.NoError(t, err)
err = os.MkdirAll(filepath.Dir(metadataFilepath), os.FileMode(0755))
assert.NoError(t, err)

// both legacy and modern metadata files exist => legacy gets deleted
err = ioutil.WriteFile(legacyMetadataFilepath, []byte{}, os.FileMode(0600))
assert.NoError(t, err)
err = ioutil.WriteFile(metadataFilepath, []byte{}, os.FileMode(0600))
assert.NoError(t, err)
ok, _ = exercise.HasLegacyMetadata()
ok, _ := exercise.HasLegacyMetadata()
assert.True(t, ok)
ok, _ = exercise.HasMetadata()
assert.True(t, ok)
str, err = exercise.MigrateLegacyMetadataFile()
assert.Equal(t, fmt.Sprintf("\nRemoved legacy metadata: %s\n", legacyMetadataFilepath), str)

stderr, err := exercise.MigrateLegacyMetadataFile()

assert.Equal(t, fmt.Sprintf("\nRemoved legacy metadata: %s\n", legacyMetadataFilepath), stderr)
assert.NoError(t, err)
ok, _ = exercise.HasLegacyMetadata()
assert.False(t, ok)
ok, _ = exercise.HasMetadata()
assert.True(t, ok)

}

0 comments on commit 8849c19

Please sign in to comment.