I'm creating a directory inside a unit test like this:
// The directory which is going to be created next to unit test executable.
const DirName string = "test-files"
Creating directory:
// Create the directory if doesn't already exist.
err := os.MkdirAll(DirName, os.ModeDir)
if err != nil {
return err
}
// Compose a sample file path to double-check its existence.
pth := DirName + string(os.PathSeparator) + "samplefile.txt"
exists, err := doesExist(pth)
if err != nil {
return err
}
And checking file existence:
// Does file exist?
func doesExist(pth string) (bool, error) {
// Check file existence
// https://stackoverflow.com/a/10510718/3405291
if _, err := os.Stat(pth); err != nil {
if os.IsNotExist(err) {
return false, nil
} else {
return true, err
}
}
// Above conditions are skipped,
// therefore file exists.
return true, nil
}
The above code returns this error:
os.Stat: permission denied
error(syscall.Errno) EACCES (13)
I can double-check that the directory is actually created. But the permissions are d---------
:
> ls -lh
d--------- 2 m3 users 6 Dec 23 20:30 test-files
How can I create the directory with the appropriate permissions?