Skip to content

Commit

Permalink
added msg constants to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
osteensco committed Oct 8, 2024
1 parent 5a87d0c commit 630dd38
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 78 deletions.
40 changes: 20 additions & 20 deletions ft/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func PassCmd(args []string) ([]string, error) {

func changeDirectory(data *CmdArgs) error {
if len(data.allPaths) == 0 {
fmt.Print(noLocationsSetMsg)
fmt.Print(NoLocationsSetMsg)
return nil
}

Expand Down Expand Up @@ -97,15 +97,15 @@ func changeDirectory(data *CmdArgs) error {
}
}

fmt.Printf(invalidDirectoryMsg, provided_string, path)
fmt.Printf(InvalidDirectoryMsg, provided_string, path)
return nil

} else {

key = provided_string
p, ok := data.allPaths[key]
if !ok {
fmt.Printf(unrecognizedKeyMsg, key, key, key)
fmt.Printf(UnrecognizedKeyMsg, key, key, key)
return nil
}

Expand All @@ -124,7 +124,7 @@ func setDirectoryVar(data *CmdArgs) error {

for k, v := range data.allPaths {
if path == v {
fmt.Printf(pathAlreadyExistsMsg, path, k, k)
fmt.Printf(PathAlreadyExistsMsg, path, k, k)
var res string
_, err := fmt.Fscan(data.rdr, &res)
if err != nil {
Expand All @@ -134,7 +134,7 @@ func setDirectoryVar(data *CmdArgs) error {
if err != nil {
return err
}
fmt.Printf(abortedOverwriteKeyMsg, key)
fmt.Printf(AbortedOverwriteKeyMsg, key)
return nil
} else {
if err != nil {
Expand All @@ -144,15 +144,15 @@ func setDirectoryVar(data *CmdArgs) error {
delete(data.allPaths, k)
data.allPaths[key] = path
dataUpdate(data.allPaths, data.file)
fmt.Printf(renamedKeyMsg, k, key, path)
fmt.Printf(RenamedKeyMsg, k, key, path)
return nil
}
}

val, ok := data.allPaths[key]
if ok {
// capture user response and act accordingly
fmt.Printf(keyAlreadyExistsMsg, key, val, key)
fmt.Printf(KeyAlreadyExistsMsg, key, val, key)
var res string
_, err := fmt.Fscan(data.rdr, &res)
if err != nil {
Expand All @@ -162,7 +162,7 @@ func setDirectoryVar(data *CmdArgs) error {
if err != nil {
return err
}
fmt.Printf(abortedOverwriteKeyMsg, key)
fmt.Printf(AbortedOverwriteKeyMsg, key)
return nil
} else {
if err != nil {
Expand All @@ -175,7 +175,7 @@ func setDirectoryVar(data *CmdArgs) error {
// key doesn't exist yet or user wants to overwrite
data.allPaths[key] = path
dataUpdate(data.allPaths, data.file)
fmt.Printf(addKeyMsg, key, path)
fmt.Printf(AddKeyMsg, key, path)
return nil
}

Expand All @@ -190,10 +190,10 @@ func removeKey(data *CmdArgs) error {

_, ok := data.allPaths[key]
if !ok {
fmt.Printf(keyDoesNotExistMsg, key)
fmt.Printf(KeyDoesNotExistMsg, key)
return nil
}
fmt.Printf(verifyRemoveMsg, key)
fmt.Printf(VerifyRemoveMsg, key)
_, err := fmt.Fscan(data.rdr, &res)
if err != nil {
return err
Expand All @@ -202,7 +202,7 @@ func removeKey(data *CmdArgs) error {
if err != nil {
return err
}
fmt.Printf(abortRemoveKeyMsg, key)
fmt.Printf(AbortRemoveKeyMsg, key)
return nil
} else {
if err != nil {
Expand All @@ -211,7 +211,7 @@ func removeKey(data *CmdArgs) error {
}
delete(data.allPaths, key)
dataUpdate(data.allPaths, data.file)
fmt.Printf(removeKeyMsg, key)
fmt.Printf(RemoveKeyMsg, key)
return nil
}

Expand All @@ -221,18 +221,18 @@ func renameKey(data *CmdArgs) error {

_, ok := data.allPaths[newKey]
if ok {
fmt.Printf(renameKeyAlreadyExistsMsg, newKey)
fmt.Printf(RenameKeyAlreadyExistsMsg, newKey)
return nil
}
path, ok := data.allPaths[originalKey]
if !ok {
fmt.Printf(renameKeyDoesNotExistMsg, originalKey)
fmt.Printf(RenameKeyDoesNotExistMsg, originalKey)
return nil
}

var res string

fmt.Printf(verifyRenameMsg, originalKey, newKey)
fmt.Printf(VerifyRenameMsg, originalKey, newKey)
_, err := fmt.Fscan(data.rdr, &res)
if err != nil {
return err
Expand All @@ -241,7 +241,7 @@ func renameKey(data *CmdArgs) error {
if err != nil {
fmt.Println("Error: ", err)
}
fmt.Printf(abortRenameKeyMsg, originalKey, newKey)
fmt.Printf(AbortRenameKeyMsg, originalKey, newKey)
return nil
} else {
if err != nil {
Expand All @@ -252,7 +252,7 @@ func renameKey(data *CmdArgs) error {
data.allPaths[newKey] = path

dataUpdate(data.allPaths, data.file)
fmt.Printf(renamedKeyMsg, originalKey, newKey, path)
fmt.Printf(RenamedKeyMsg, originalKey, newKey, path)
return nil
}

Expand All @@ -279,12 +279,12 @@ func showDirectoryVar(data *CmdArgs) error {
v, _ := pathMaps[k]

if strings.Compare(v, dir) == 0 {
fmt.Printf("Directory %s is saved to key : %s \n", dir, k)
fmt.Printf(IsKeyMsg, dir, k)
return nil
}
}

fmt.Printf("No key was found for the specified path: %s \n", dir)
fmt.Printf(IsNotKeyMsg, dir)
return nil
}

Expand Down
50 changes: 16 additions & 34 deletions ft/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestChangeDirectory(t *testing.T) {
{
name: "1. Valid key provided, standalone.",
command: []string{"_", "testKey"},
expected: tmpdir,
expected: fmt.Sprintln(tmpdir),
allPaths: map[string]string{
"testKey": tmpdir,
},
Expand All @@ -90,7 +90,7 @@ func TestChangeDirectory(t *testing.T) {
{
name: "2. Valid key provided, evaluate path.",
command: []string{"_", "testKey/subdir"},
expected: tmpdir2,
expected: fmt.Sprintln(tmpdir2),
allPaths: map[string]string{
"testKey": tmpdir,
},
Expand All @@ -100,7 +100,7 @@ func TestChangeDirectory(t *testing.T) {
{
name: "3. Invalid key provided.",
command: []string{"_", "testKye"},
expected: "Did not recognize key 'testKye', use 'ft -ls' to see all saved destinations. If this is a relative path use './testKye' or 'testKye/'. ",
expected: fmt.Sprintf(UnrecognizedKeyMsg, "testKye", "testKye", "testKye"),
allPaths: map[string]string{
"testKey": tmpdir,
},
Expand All @@ -110,7 +110,7 @@ func TestChangeDirectory(t *testing.T) {
{
name: "4. Invalid key provided, evaluate path.",
command: []string{"_", "testKye/subdir"},
expected: "Provided path 'testKye/subdir' evaluates to 'testKye/subdir' which is not a valid directory. Use 'ft -ls' to see all saved destinations. ",
expected: fmt.Sprintf(InvalidDirectoryMsg, "testKye/subdir", "testKye/subdir"),
allPaths: map[string]string{
"testKey": tmpdir,
},
Expand All @@ -120,7 +120,7 @@ func TestChangeDirectory(t *testing.T) {
}

for _, tt := range tests {

t.Log(tt.name)
data := NewCmdArgs(
tt.command,
tt.allPaths,
Expand Down Expand Up @@ -154,58 +154,42 @@ func TestChangeDirectory(t *testing.T) {
os.Stdout = stdout
os.Stderr = stderr
actual := <-outChan
actual = strings.Trim(actual, "\n")
fmt.Println(actual)

if actual != tt.expected {
fmt.Println(tt.name)
t.Errorf("Expected %s, got %s", tt.expected, actual)
t.Errorf("Expected: %q, got: %q", tt.expected, actual)
}
}
}

func TestShowDirectoryVar(t *testing.T) {
tmpfile, err := os.CreateTemp("", "testdata.bin")
if err != nil {
t.Fatalf("Failed to create file: %v", err)
}
defer os.Remove(tmpfile.Name())
defer tmpfile.Close()

CWD, err := os.Getwd()
if err != nil {
t.Fatal(err)
}

tests := []struct {
name string
command []string
paths map[string]string
expected string
wanterr bool
}{
{
name: "1. Check the directory var using -is",
command: []string{"-is"},
expected: "testKey",
paths: map[string]string{"testKey": CWD},
expected: fmt.Sprintf(IsKeyMsg, CWD, "testKey"),
wanterr: false,
},
}

for _, tt := range tests {
testData := NewCmdArgs(
[]string{"-set", "testKey"},
make(map[string]string),
tmpfile,
nil,
)

err := setDirectoryVar(testData)
if err != nil {
t.Errorf("Error setting directory var: %v", err)
}

t.Log(tt.name)
data := NewCmdArgs(
tt.command,
testData.allPaths,
tmpfile,
tt.paths,
nil,
nil,
)

Expand Down Expand Up @@ -234,10 +218,8 @@ func TestShowDirectoryVar(t *testing.T) {
os.Stdout = old
actual := <-outChan

actual = strings.Trim(actual, "\n")

if len(actual) == 0 {
t.Errorf("Did not get the expected key")
if actual != tt.expected {
t.Errorf("Expected message: %q but got message: %q", tt.expected, actual)
}
}
}
Expand Down
38 changes: 19 additions & 19 deletions ft/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,24 @@ var Version string = "development"
// default value for logo
var Logo string = "fastTravelCLI"

// responses
// messages
const (
noLocationsSetMsg = "No fast travel locations set, set locations by navigating to desired destination directory and using 'ft -set <key>' \n"
invalidDirectoryMsg = "Provided path '%s' evaluates to '%s' which is not a valid directory. Use 'ft -ls' to see all saved destinations. \n"
unrecognizedKeyMsg = "Did not recognize key '%s', use 'ft -ls' to see all saved destinations. If this is a relative path use './%s' or '%s/'. \n"
pathAlreadyExistsMsg = "Path '%s' already exists with key '%s', overwrite key '%s' \n"
abortedOverwriteKeyMsg = "Aborted overwriting of key '%s' \n"
renamedKeyMsg = "Renamed key '%s' to '%s' whose value is '%s' \n"
keyAlreadyExistsMsg = "Key '%s' already exists with value '%s', overwrite key '%s'?(y/n) "
addKeyMsg = "Added destination '%s': '%s' \n"
keyDoesNotExistMsg = "Key '%s' does not exist. Run 'ft -ls' to see all keys. \n"
verifyRemoveMsg = "Are you sure you want to remove the key '%s'? (y/n) "
abortRemoveKeyMsg = "Aborted removal of key '%s' \n"
removeKeyMsg = "Removed '%s' destination \n"
renameKeyAlreadyExistsMsg = "Key '%s' already exists, please choose something else. \n"
renameKeyDoesNotExistMsg = "Cannot rename '%s', key does not exist. Run 'ft -ls' to see all keys. \n"
verifyRenameMsg = "Are you sure you want to rename the key '%s' to '%s'? (y/n) "
abortRenameKeyMsg = "Aborted renaming of key '%s' to '%s'. \n"
NoLocationsSetMsg = "No fast travel locations set, set locations by navigating to desired destination directory and using 'ft -set <key>' \n"
InvalidDirectoryMsg = "Provided path '%s' evaluates to '%s' which is not a valid directory. Use 'ft -ls' to see all saved destinations. \n"
UnrecognizedKeyMsg = "Did not recognize key '%s', use 'ft -ls' to see all saved destinations. If this is a relative path use './%s' or '%s/'. \n"
PathAlreadyExistsMsg = "Path '%s' already exists with key '%s', overwrite key '%s' \n"
AbortedOverwriteKeyMsg = "Aborted overwriting of key '%s' \n"
RenamedKeyMsg = "Renamed key '%s' to '%s' whose value is '%s' \n"
KeyAlreadyExistsMsg = "Key '%s' already exists with value '%s', overwrite key '%s'?(y/n) "
AddKeyMsg = "Added destination '%s': '%s' \n"
KeyDoesNotExistMsg = "Key '%s' does not exist. Run 'ft -ls' to see all keys. \n"
VerifyRemoveMsg = "Are you sure you want to remove the key '%s'? (y/n) "
AbortRemoveKeyMsg = "Aborted removal of key '%s' \n"
RemoveKeyMsg = "Removed '%s' destination \n"
RenameKeyAlreadyExistsMsg = "Key '%s' already exists, please choose something else. \n"
RenameKeyDoesNotExistMsg = "Cannot rename '%s', key does not exist. Run 'ft -ls' to see all keys. \n"
VerifyRenameMsg = "Are you sure you want to rename the key '%s' to '%s'? (y/n) "
AbortRenameKeyMsg = "Aborted renaming of key '%s' to '%s'. \n"
IsKeyMsg = "Directory %s is saved to key : %s \n"
IsNotKeyMsg = "No key was found for the specified path: %s \n"
)

//
3 changes: 1 addition & 2 deletions ft/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func ReadMap(file *os.File) (map[string]string, error) {
// read entire file into memory
_, err = file.Read(buff)
if err != nil {
fmt.Println("Error reading file into buffer: ", err)
os.Exit(1)
return pathMap, errors.New(fmt.Sprint("Error reading file into buffer: ", err))
}

// key length integer should always fit in 1 byte
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {

err = exeCmd(data)
if err != nil {
fmt.Println("fastTravel returned an error: ", err)
fmt.Println("fastTravelCLI returned an error: ", err)
return
}

Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestMainFunc(t *testing.T) {
{
name: "2. Check set command.",
args: []string{"ft", "-set", "key"},
expected: fmt.Sprintf("Added destination 'key': '%s' \n", tmpdir),
expected: fmt.Sprintf(ft.AddKeyMsg, "key", tmpdir),
wantErr: false,
},
{
Expand All @@ -87,7 +87,7 @@ func TestMainFunc(t *testing.T) {
{
name: "4. Check cd command with bad key.",
args: []string{"ft", "badkey"},
expected: "Did not recognize key 'badkey', use 'ft -ls' to see all saved destinations. If this is a relative path use './badkey' or 'badkey/'. \n",
expected: fmt.Sprintf(ft.UnrecognizedKeyMsg, "badkey", "badkey", "badkey"),
wantErr: false,
},
{
Expand Down

0 comments on commit 630dd38

Please sign in to comment.