Skip to content

Commit

Permalink
Add state type return to MigrateLegacyMetadataFile
Browse files Browse the repository at this point in the history
Rather than printing from inside the migration, return a status type
that can be inferred.
  • Loading branch information
jdsutherland committed Aug 29, 2018
1 parent c770724 commit 9cd6e4c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
}

exercise := workspace.NewExerciseFromDir(exerciseDir)
if err = exercise.MigrateLegacyMetadataFile(); err != nil {
if _, err = exercise.MigrateLegacyMetadataFile(); err != nil {
return err
}
solution, err := workspace.NewSolution(exerciseDir)
Expand Down
28 changes: 22 additions & 6 deletions workspace/exercise.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,44 @@ func (e Exercise) HasLegacyMetadata() (bool, error) {
return false, err
}

// MigrationStatus represents the result of migrating a legacy metadata file.
type MigrationStatus int

// MigrationStatus
const (
MigrationStatusMkdirError MigrationStatus = iota
MigrationStatusRenameError
MigrationStatusRemoveError
MigrationStatusNoop
MigrationStatusMigrated
MigrationStatusRemoved
)

// MigrateLegacyMetadataFile migrates a legacy metadata to the modern location.
// This is a noop if the metadata file isn't legacy.
// If both legacy and modern metadata files exist, the legacy file will be deleted.
func (e Exercise) MigrateLegacyMetadataFile() error {
func (e Exercise) MigrateLegacyMetadataFile() (MigrationStatus, error) {
if ok, _ := e.HasLegacyMetadata(); !ok {
return nil
return MigrationStatusNoop, nil
}
var status MigrationStatus
legacyMetadataFilepath := e.LegacyMetadataFilepath()
if err := os.MkdirAll(
filepath.Join(filepath.Dir(legacyMetadataFilepath), ignoreSubdir),
os.FileMode(0755)); err != nil {
return err
return MigrationStatusMkdirError, err
}
if ok, _ := e.HasMetadata(); !ok {
if err := os.Rename(legacyMetadataFilepath, e.MetadataFilepath()); err != nil {
return err
return MigrationStatusRenameError, err
}
status = MigrationStatusMigrated
}
if ok, _ := e.HasLegacyMetadata(); ok {
if err := os.Remove(legacyMetadataFilepath); err != nil {
return err
return MigrationStatusRemoveError, err
}
status = MigrationStatusRemoved
}
return nil
return status, nil
}
9 changes: 6 additions & 3 deletions workspace/exercise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func TestMigrateLegacyMetadataFileWithoutLegacy(t *testing.T) {
ok, _ = exercise.HasMetadata()
assert.True(t, ok)

err = exercise.MigrateLegacyMetadataFile()
status, err := exercise.MigrateLegacyMetadataFile()
assert.Equal(t, MigrationStatus(MigrationStatusNoop), status)
assert.Nil(t, err)

ok, _ = exercise.HasLegacyMetadata()
Expand All @@ -113,7 +114,8 @@ func TestMigrateLegacyMetadataFileWithLegacy(t *testing.T) {
ok, _ = exercise.HasMetadata()
assert.False(t, ok)

err = exercise.MigrateLegacyMetadataFile()
status, err := exercise.MigrateLegacyMetadataFile()
assert.Equal(t, MigrationStatus(MigrationStatusMigrated), status)
assert.NoError(t, err)

ok, _ = exercise.HasLegacyMetadata()
Expand Down Expand Up @@ -145,7 +147,8 @@ func TestMigrateLegacyMetadataFileWithLegacyAndModern(t *testing.T) {
ok, _ = exercise.HasMetadata()
assert.True(t, ok)

err = exercise.MigrateLegacyMetadataFile()
status, err := exercise.MigrateLegacyMetadataFile()
assert.Equal(t, MigrationStatus(MigrationStatusRemoved), status)
assert.NoError(t, err)

ok, _ = exercise.HasLegacyMetadata()
Expand Down

0 comments on commit 9cd6e4c

Please sign in to comment.