dimanche 4 novembre 2018

How can I test Golang code that uses users.Lookup without having it read real users?

I'm trying to write tests for this function in Go:

func (b *LinuxBackend) GetSSHUser(name string) users.SSHUser {
    // Get the user from the system
    usr, err := user.Lookup(name)
    if err != nil {
        panic(err)
    }

    return users.SSHUser{ User: usr }
}

For other parts of my code, I abstracted the underlying file system using Afero to create an in-memory file system, creating the necessary files (including /etc/passwd) so the tests can run.

I can't do that for user.Lookup(), though, which means it will try to fetch the non-existent test users from the real file system.

Is there a way I can somehow make Go use the in-memory file system for user.Lookup, or write the test in some other way so that user.Lookup doesn't use the real file system?

Aucun commentaire:

Enregistrer un commentaire