main: Mock file system and add getFile tests

This uses afero to proxy all file system accesses and then injects an
in-memory file system in order to test the getFile function.
This commit is contained in:
Johannes Marbach
2021-12-30 14:25:37 +01:00
parent 206e75c597
commit 91529b736e
4 changed files with 603 additions and 26 deletions

View File

@@ -4,6 +4,9 @@
package main
import (
"github.com/spf13/afero"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/pkgs/misc"
"reflect"
"strings"
"testing"
)
@@ -80,3 +83,123 @@ func TestGetModuleDeps(t *testing.T) {
}
}
}
func setUpFakeFs() {
fs := afero.NewMemMapFs()
initFs(fs)
fs.Mkdir("/foo", 0755)
afero.WriteFile(fs, "/foo/foo.txt", []byte("foo"), 0644)
fs.Mkdir("/foo/bar", 0755)
afero.WriteFile(fs, "/foo/bar/bar.txt", []byte("bar"), 0644)
afero.WriteFile(fs, "/foo/bar/foo.txt", []byte("foo"), 0644)
}
func TestGetFileWithFilePath(t *testing.T) {
setUpFakeFs()
actual := make(misc.StringSet)
file := "/foo/bar/bar.txt"
if err := getFile(actual, file, true); err != nil {
t.Errorf("Unexpected error when collecting file %q, error: %q", file, err)
}
expected := make(misc.StringSet)
expected["/foo/bar/bar.txt"] = false
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Expected: %v, got: %v", expected, actual)
}
}
func TestGetFileWithGlobThatExpandsToSingleFile(t *testing.T) {
setUpFakeFs()
actual := make(misc.StringSet)
file := "/foo/bar/bar.*"
if err := getFile(actual, file, true); err != nil {
t.Errorf("Unexpected error when collecting file %q, error: %q", file, err)
}
expected := make(misc.StringSet)
expected["/foo/bar/bar.txt"] = false
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Expected: %v, got: %v", expected, actual)
}
}
func TestGetFileWithGlobThatExpandsToMultipleFiles(t *testing.T) {
setUpFakeFs()
actual := make(misc.StringSet)
file := "/foo/bar/*.txt"
if err := getFile(actual, file, true); err != nil {
t.Errorf("Unexpected error when collecting file %q, error: %q", file, err)
}
expected := make(misc.StringSet)
expected["/foo/bar/bar.txt"] = false
expected["/foo/bar/foo.txt"] = false
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Expected: %v, got: %v", expected, actual)
}
}
func TestGetFileWithDirectoryPath(t *testing.T) {
setUpFakeFs()
actual := make(misc.StringSet)
file := "/foo"
if err := getFile(actual, file, true); err != nil {
t.Errorf("Unexpected error when collecting file %q, error: %q", file, err)
}
expected := make(misc.StringSet)
expected["/foo/foo.txt"] = false
expected["/foo/bar/bar.txt"] = false
expected["/foo/bar/foo.txt"] = false
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Expected: %v, got: %v", expected, actual)
}
}
func TestGetFileWithGlobThatExpandsToSingleDirectory(t *testing.T) {
setUpFakeFs()
actual := make(misc.StringSet)
file := "/foo/b*"
if err := getFile(actual, file, true); err != nil {
t.Errorf("Unexpected error when collecting file %q, error: %q", file, err)
}
expected := make(misc.StringSet)
expected["/foo/bar/bar.txt"] = false
expected["/foo/bar/foo.txt"] = false
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Expected: %v, got: %v", expected, actual)
}
}
func TestGetFileWithGlobThatExpandsToDirectoriesAndFiles(t *testing.T) {
setUpFakeFs()
actual := make(misc.StringSet)
file := "/foo/*"
if err := getFile(actual, file, true); err != nil {
t.Errorf("Unexpected error when collecting file %q, error: %q", file, err)
}
expected := make(misc.StringSet)
expected["/foo/foo.txt"] = false
expected["/foo/bar/bar.txt"] = false
expected["/foo/bar/foo.txt"] = false
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Expected: %v, got: %v", expected, actual)
}
}