mirror of
https://github.com/moby/moby.git
synced 2026-01-11 02:31:44 +00:00
It has no external consumers, is written with specific behavior (including some potentially surprising behavior), making it not a good candidate to carry in the module. This moves it internal to the daemon as a non-exported utility, so that it's only accessible where it's currently used. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
40 lines
1.0 KiB
Go
40 lines
1.0 KiB
Go
package daemon
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"gotest.tools/v3/assert"
|
|
)
|
|
|
|
func TestCreateIfNotExists(t *testing.T) {
|
|
t.Run("directory", func(t *testing.T) {
|
|
toCreate := filepath.Join(t.TempDir(), "tocreate")
|
|
|
|
err := createIfNotExists(toCreate, true)
|
|
assert.NilError(t, err)
|
|
|
|
fileinfo, err := os.Stat(toCreate)
|
|
assert.NilError(t, err, "Did not create destination")
|
|
assert.Assert(t, fileinfo.IsDir(), "Should have been a dir, seems it's not")
|
|
|
|
err = createIfNotExists(toCreate, true)
|
|
assert.NilError(t, err, "Should not fail if already exists")
|
|
})
|
|
t.Run("file", func(t *testing.T) {
|
|
toCreate := filepath.Join(t.TempDir(), "file/to/create")
|
|
|
|
err := createIfNotExists(toCreate, false)
|
|
assert.NilError(t, err)
|
|
|
|
fileinfo, err := os.Stat(toCreate)
|
|
assert.NilError(t, err, "Did not create destination")
|
|
|
|
assert.Assert(t, !fileinfo.IsDir(), "Should have been a file, but created a directory")
|
|
|
|
err = createIfNotExists(toCreate, true)
|
|
assert.NilError(t, err, "Should not fail if already exists")
|
|
})
|
|
}
|