lundi 2 novembre 2020

what's the best practice for testing deleting a directory?

I have a cli that I am making that is more for learning purposes and creating my own cli, that does stuff. Anyways, I am testing the delete function and it works fine and gives me the right answer. However, I don't believe that it is the best practice and was wondering if you could let me know if it's ok, or not.

Test file

func TestDeleteConfig(t *testing.T) {
    err := cm.DeleteConfig()
    if err != nil {
        t.Errorf("error when deleting the folder: %s", err)
    }

    usr, err := user.Current()
    if err != nil {
        t.Errorf("error when getting user current: %s", err)
    }

    fp := filepath.Join(usr.HomeDir, ".config", "godot", "config.json")
    fmt.Println("the path of the config file", fp)

    if _, e := os.Stat(fp); !os.IsNotExist(e) {
        t.Errorf("error path still exists: %v", e)
    }
}

function being tested

func DeleteConfig() error {

    usr, err := user.Current()
    if err != nil {
        return err
    }

    err = os.RemoveAll(filepath.Join(usr.HomeDir, ".config", "godot"))
    if err != nil {
        return err
    }

    return nil
}

The problem is that I don't want the DeleteConfig() to take any arguments as this is a hard path. I have a separate function for deleting a single file in which the help with this will solve that as well func DeleteFile(p string) error {}.

So for testing purposes should I just create a foo directory at a separate path (within the test), delete said directory, and assume that if it works on foo path then it should work with the godot directory?

Aucun commentaire:

Enregistrer un commentaire