Example #1
0
func TestRWVFS(t *testing.T) {
	tmpdir, err := ioutil.TempDir("", "rwvfs-test-")
	if err != nil {
		t.Fatal("TempDir", err)
	}
	defer os.RemoveAll(tmpdir)

	h := http.Handler(rwvfs.HTTPHandler(rwvfs.Map(map[string]string{}), nil))
	httpServer := httptest.NewServer(h)
	defer httpServer.Close()
	httpURL, err := url.Parse(httpServer.URL)
	if err != nil {
		t.Fatal(err)
	}

	tests := []struct {
		fs rwvfs.FileSystem
	}{
		{rwvfs.OS(tmpdir)},
		{rwvfs.Map(map[string]string{})},
		{rwvfs.Sub(rwvfs.Map(map[string]string{}), "/x")},
		{rwvfs.HTTP(httpURL, nil)},
		{rwvfs.Union(rwvfs.Map(map[string]string{}), rwvfs.Map(map[string]string{}))},
	}
	for _, test := range tests {
		testutil.Write(t, test.fs)
		testutil.Mkdir(t, test.fs)
		testutil.MkdirAll(t, test.fs)
		testutil.Glob(t, test.fs)
	}
}
func TestBuildDataService_ListAll(t *testing.T) {
	setup()
	defer teardown()

	pathPrefix := urlPath(t, router.RepoBuildDataEntry, map[string]string{"RepoSpec": "r.com/x", "Rev": "c", "Path": "."})
	fs := rwvfs.Map(map[string]string{
		"a":     "a",
		"b/c":   "c",
		"b/d/e": "e",
	})
	mux.Handle(pathPrefix+"/", http.StripPrefix(pathPrefix, rwvfs.HTTPHandler(fs, nil)))

	fs, err := client.BuildData.FileSystem(RepoRevSpec{RepoSpec: RepoSpec{URI: "r.com/x"}, Rev: "c"})
	if err != nil {
		t.Fatal(err)
	}

	entries, err := rwvfs.StatAllRecursive(".", rwvfs.Walkable(fs))
	if err != nil {
		t.Fatalf("StatAllRecursive returned error: %v", err)
	}

	names := fileInfoNames(entries)
	wantNames := []string{".", "a", "b", "b/c", "b/d", "b/d/e"}
	sort.Strings(names)
	sort.Strings(wantNames)
	if !reflect.DeepEqual(names, wantNames) {
		t.Errorf("got entry names %v, want %v", names, wantNames)
	}
}
Example #3
0
func TestHTTP_BaseURL(t *testing.T) {
	m := map[string]string{"b/c": "c"}
	mapFS := rwvfs.Map(m)

	prefix := "/foo/bar/baz"

	h := http.Handler(http.StripPrefix(prefix, rwvfs.HTTPHandler(mapFS, nil)))
	httpServer := httptest.NewServer(h)
	defer httpServer.Close()
	httpURL, err := url.Parse(httpServer.URL + prefix)
	if err != nil {
		t.Fatal(err)
	}

	fs := rwvfs.HTTP(httpURL, nil)

	if err := rwvfs.MkdirAll(fs, "b"); err != nil {
		t.Errorf("MkdirAll %q: %s", "b", err)
	}

	fis, err := fs.ReadDir("b")
	if err != nil {
		t.Fatal(err)
	}
	if len(fis) != 1 {
		t.Errorf("got len(fis) == %d, want 1", len(fis))
	}
	if wantName := "c"; fis[0].Name() != wantName {
		t.Errorf("got name == %q, want %q", fis[0].Name(), wantName)
	}
}
Example #4
0
func TestUnion(t *testing.T) {
	m1 := map[string]string{"foo/file": ""}
	m2 := map[string]string{"bar/file": ""}
	u := rwvfs.Union(rwvfs.Map(m1), rwvfs.Map(m2))

	infos, err := u.ReadDir("/")
	if err != nil {
		t.Fatal(err)
	}
	if got, want := len(infos), 2; got != want {
		t.Errorf(`ReadDir: got %d, want %d`, got, want)
	}

	testCreate(t, u, "test", m1)
	testCreate(t, u, "foo/test", m1)
	testCreate(t, u, "bar/test", m2)
}
Example #5
0
func TestMap_MkdirAllWithRootNotExists(t *testing.T) {
	m := map[string]string{}
	fs := rwvfs.Sub(rwvfs.Map(m), "x")

	paths := []string{"a/b", "/c/d"}
	for _, path := range paths {
		if err := rwvfs.MkdirAll(fs, path); err != nil {
			t.Errorf("MkdirAll %q: %s", path, err)
		}
	}
}
Example #6
0
func newTestFS() rwvfs.WalkableFileSystem {
	switch *fsType {
	case "map":
		fs := rwvfs.Map(map[string]string{})
		return rwvfs.Walkable(rwvfs.Sub(fs, "/testdata"))
	case "os":
		tmpDir, err := ioutil.TempDir("", "srclib-test")
		if err != nil {
			log.Fatal(err)
		}
		fs := rwvfs.OS(tmpDir)
		setCreateParentDirs(fs)
		return rwvfs.Walkable(fs)
	default:
		log.Fatalf("unrecognized -test.fs option: %q", *fsType)
		panic("unreachable")
	}
}
Example #7
0
// This just tests the module structure is intact
func TestModule(t *testing.T) {
	var (
		m Module
		r testResponse
	)

	m = NewTestModule()
	in := NewConfigInput(map[string]interface{}{"input": "test string"})

	m.Configure(in)

	fs := rwvfs.Map(map[string]string{})
	m.Run(fs, &r)

	assert.Equal(t, r.level, "info")
	assert.Equal(t, "[Run completed successfully with data: test string]", r.message)
	assert.True(t, r.ok)
	assert.True(t, r.callbacks)
}
Example #8
0
func TestMap_Walk2(t *testing.T) {
	m := map[string]string{"a/b/c/d": "a"}
	mapFS := rwvfs.Map(m)

	var names []string
	w := fs.WalkFS(".", rwvfs.Walkable(rwvfs.Sub(mapFS, "a/b")))
	for w.Step() {
		if err := w.Err(); err != nil {
			t.Fatalf("walk path %q: %s", w.Path(), err)
		}
		names = append(names, w.Path())
	}

	wantNames := []string{".", "c", "c/d"}
	sort.Strings(names)
	sort.Strings(wantNames)
	if !reflect.DeepEqual(names, wantNames) {
		t.Errorf("got entry names %v, want %v", names, wantNames)
	}
}
Example #9
0
func TestSub(t *testing.T) {
	m := rwvfs.Map(map[string]string{})
	sub := rwvfs.Sub(m, "/sub")

	err := sub.Mkdir("/")
	if err != nil {
		t.Fatal(err)
	}
	testutil.IsDir(t, "sub", m, "/sub")

	f, err := sub.Create("f1")
	f.Close()
	if err != nil {
		t.Fatal(err)
	}
	testutil.IsFile(t, "sub", m, "/sub/f1")

	f, err = sub.Create("/f2")
	f.Close()
	if err != nil {
		t.Fatal(err)
	}
	testutil.IsFile(t, "sub", m, "/sub/f2")

	err = sub.Mkdir("/d1")
	if err != nil {
		t.Fatal(err)
	}
	testutil.IsDir(t, "sub", m, "/sub/d1")

	err = sub.Mkdir("/d2")
	if err != nil {
		t.Fatal(err)
	}
	testutil.IsDir(t, "sub", m, "/sub/d2")
}
Example #10
0
func TestTargetsNeedingBuild(t *testing.T) {
	tests := map[string]struct {
		mf    *Makefile
		fs    FileSystem
		goals []string
		// If afterMake is set, the test will run 'make' once,
		// call afterMake with fs, and then check the test
		// conditions.
		afterMake                  func(fs FileSystem) error
		wantErr                    error
		wantTargetSetsNeedingBuild [][]string
	}{
		"do nothing if empty": {
			mf: &Makefile{},
			wantTargetSetsNeedingBuild: [][]string{},
		},
		"return error if target isn't defined in Makefile": {
			mf:      &Makefile{},
			goals:   []string{"x"},
			wantErr: errNoRuleToMakeTarget("x"),
		},
		"don't build target that already exists": {
			mf:    &Makefile{Rules: []Rule{&BasicRule{TargetFile: "x"}}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{"x": ""})),
			goals: []string{"x"},
			wantTargetSetsNeedingBuild: [][]string{},
		},
		"build target that doesn't exist": {
			mf:    &Makefile{Rules: []Rule{&BasicRule{TargetFile: "x"}}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x"},
			wantTargetSetsNeedingBuild: [][]string{{"x"}},
		},
		"build only target with stale prereq": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x", PrereqFiles: []string{"x1"}},
				&BasicRule{TargetFile: "y", PrereqFiles: []string{"y1"}},
			}},
			fs: newModTimeFileSystem(rwvfs.Map(map[string]string{
				"x": "", "x1": "", "y": "", "y1": "",
			})),
			afterMake: func(fs FileSystem) error {
				w, err := fs.Create("x1")
				if err != nil {
					return err
				}
				defer w.Close()
				if _, err := io.WriteString(w, "modified"); err != nil {
					return err
				}
				return nil
			},
			goals: []string{"x", "y"},
			wantTargetSetsNeedingBuild: [][]string{{"x"}},
		},
		"build targets recursively that don't exist": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0", PrereqFiles: []string{"x1"}},
				&BasicRule{TargetFile: "x1"},
			}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x0"},
			wantTargetSetsNeedingBuild: [][]string{{"x1"}, {"x0"}},
		},

		"don't build targets that don't directly achieve goals (simple)": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0"},
				&BasicRule{TargetFile: "x1"},
			}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x0"},
			wantTargetSetsNeedingBuild: [][]string{{"x0"}},
		},
		"don't build targets that don't achieve goals (complex)": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0", PrereqFiles: []string{"y"}},
				&BasicRule{TargetFile: "x1"},
				&BasicRule{TargetFile: "y"},
			}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x0"},
			wantTargetSetsNeedingBuild: [][]string{{"y"}, {"x0"}},
		},
		"don't build targets that don't achieve goals (even when a common prereq is satisfied)": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0", PrereqFiles: []string{"y"}},
				&BasicRule{TargetFile: "x1", PrereqFiles: []string{"y"}},
				&BasicRule{TargetFile: "y"},
			}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x0"},
			wantTargetSetsNeedingBuild: [][]string{{"y"}, {"x0"}},
		},

		"don't build goal targets more than once": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0"},
			}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x0", "x0"},
			wantTargetSetsNeedingBuild: [][]string{{"x0"}},
		},
		"don't build any targets more than once": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0", PrereqFiles: []string{"y"}},
				&BasicRule{TargetFile: "x1", PrereqFiles: []string{"y"}},
				&BasicRule{TargetFile: "y"},
			}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x0", "x1"},
			wantTargetSetsNeedingBuild: [][]string{{"y"}, {"x0", "x1"}},
		},
		"detect 1-cycles": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0", PrereqFiles: []string{"x0"}},
			}},
			fs:      NewFileSystem(rwvfs.Map(map[string]string{})),
			goals:   []string{"x0"},
			wantErr: errCircularDependency("x0", []string{"x0"}),
		},
		"detect 2-cycles": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0", PrereqFiles: []string{"x1"}},
				&BasicRule{TargetFile: "x1", PrereqFiles: []string{"x0"}},
			}},
			fs:      NewFileSystem(rwvfs.Map(map[string]string{})),
			goals:   []string{"x0"},
			wantErr: errCircularDependency("x0", []string{"x1"}),
		},
		"re-build .PHONY target": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: ".PHONY", PrereqFiles: []string{"all"}},
				&BasicRule{TargetFile: "all", PrereqFiles: []string{"file"}},
			}},
			fs: newModTimeFileSystem(rwvfs.Map(map[string]string{
				"all": "", "file": "",
			})),
			goals: []string{"all"},
			wantTargetSetsNeedingBuild: [][]string{{"all"}},
		},
		"re-build .PHONY pre-requisite": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: ".PHONY", PrereqFiles: []string{"all", "compile"}},
				&BasicRule{TargetFile: "all", PrereqFiles: []string{"compile"}},
				&BasicRule{TargetFile: "compile", PrereqFiles: []string{"file"}},
			}},
			fs: newModTimeFileSystem(rwvfs.Map(map[string]string{
				"all": "", "compile": "", "file": "",
			})),
			goals: []string{"all"},
			wantTargetSetsNeedingBuild: [][]string{{"compile"}, {"all"}},
		},
	}

	for label, test := range tests {
		conf := &Config{FS: test.fs}
		mk := conf.NewMaker(test.mf, test.goals...)
		if test.afterMake != nil {
			if err := mk.Run(); err != nil {
				t.Fatal(err)
			}
			if err := test.afterMake(test.fs); err != nil {
				t.Fatal(err)
			}
		}
		targetSets, err := mk.TargetSetsNeedingBuild()
		if !reflect.DeepEqual(err, test.wantErr) {
			if test.wantErr == nil {
				t.Errorf("%s: TargetsNeedingBuild(%q): error: %s", label, test.goals, err)
				continue
			} else {
				t.Errorf("%s: TargetsNeedingBuild(%q): error: got %q, want %q", label, test.goals, err, test.wantErr)
				continue
			}
		}

		// sort so that test is deterministic
		for _, ts := range targetSets {
			sort.Strings(ts)
		}
		if !reflect.DeepEqual(targetSets, test.wantTargetSetsNeedingBuild) {
			t.Errorf("%s: got targetSets needing build %v, want %v", label, targetSets, test.wantTargetSetsNeedingBuild)
		}
	}
}
func idxUnitStore() UnitStoreImporter {
	fs := rwvfs.Map(map[string]string{})
	return newIndexedUnitStore(fs, "")
}
func newFSUnitStore() UnitStoreImporter {
	fs := rwvfs.Map(map[string]string{})
	return &fsUnitStore{fs: fs}
}
Example #13
0
func TestTargetsNeedingBuild(t *testing.T) {
	tests := map[string]struct {
		mf                         *Makefile
		fs                         FileSystem
		goals                      []string
		wantErr                    error
		wantTargetSetsNeedingBuild [][]string
	}{
		"do nothing if empty": {
			mf: &Makefile{},
			wantTargetSetsNeedingBuild: [][]string{},
		},
		"return error if target isn't defined in Makefile": {
			mf:      &Makefile{},
			goals:   []string{"x"},
			wantErr: errNoRuleToMakeTarget("x"),
		},
		"don't build target that already exists": {
			mf:    &Makefile{Rules: []Rule{&BasicRule{TargetFile: "x"}}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{"x": ""})),
			goals: []string{"x"},
			wantTargetSetsNeedingBuild: [][]string{},
		},
		"build target that doesn't exist": {
			mf:    &Makefile{Rules: []Rule{&BasicRule{TargetFile: "x"}}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x"},
			wantTargetSetsNeedingBuild: [][]string{{"x"}},
		},
		"build targets recursively that don't exist": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0", PrereqFiles: []string{"x1"}},
				&BasicRule{TargetFile: "x1"},
			}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x0"},
			wantTargetSetsNeedingBuild: [][]string{{"x1"}, {"x0"}},
		},

		"don't build targets that don't directly achieve goals (simple)": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0"},
				&BasicRule{TargetFile: "x1"},
			}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x0"},
			wantTargetSetsNeedingBuild: [][]string{{"x0"}},
		},
		"don't build targets that don't achieve goals (complex)": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0", PrereqFiles: []string{"y"}},
				&BasicRule{TargetFile: "x1"},
				&BasicRule{TargetFile: "y"},
			}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x0"},
			wantTargetSetsNeedingBuild: [][]string{{"y"}, {"x0"}},
		},
		"don't build targets that don't achieve goals (even when a common prereq is satisfied)": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0", PrereqFiles: []string{"y"}},
				&BasicRule{TargetFile: "x1", PrereqFiles: []string{"y"}},
				&BasicRule{TargetFile: "y"},
			}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x0"},
			wantTargetSetsNeedingBuild: [][]string{{"y"}, {"x0"}},
		},

		"don't build goal targets more than once": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0"},
			}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x0", "x0"},
			wantTargetSetsNeedingBuild: [][]string{{"x0"}},
		},
		"don't build any targets more than once": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0", PrereqFiles: []string{"y"}},
				&BasicRule{TargetFile: "x1", PrereqFiles: []string{"y"}},
				&BasicRule{TargetFile: "y"},
			}},
			fs:    NewFileSystem(rwvfs.Map(map[string]string{})),
			goals: []string{"x0", "x1"},
			wantTargetSetsNeedingBuild: [][]string{{"y"}, {"x0", "x1"}},
		},
		"detect 1-cycles": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0", PrereqFiles: []string{"x0"}},
			}},
			fs:      NewFileSystem(rwvfs.Map(map[string]string{})),
			goals:   []string{"x0"},
			wantErr: errCircularDependency("x0", []string{"x0"}),
		},
		"detect 2-cycles": {
			mf: &Makefile{Rules: []Rule{
				&BasicRule{TargetFile: "x0", PrereqFiles: []string{"x1"}},
				&BasicRule{TargetFile: "x1", PrereqFiles: []string{"x0"}},
			}},
			fs:      NewFileSystem(rwvfs.Map(map[string]string{})),
			goals:   []string{"x0"},
			wantErr: errCircularDependency("x0", []string{"x1"}),
		},
	}

	for label, test := range tests {
		conf := &Config{FS: test.fs}
		mk := conf.NewMaker(test.mf, test.goals...)
		targetSets, err := mk.TargetSetsNeedingBuild()
		if !reflect.DeepEqual(err, test.wantErr) {
			if test.wantErr == nil {
				t.Errorf("%s: TargetsNeedingBuild(%q): error: %s", label, test.goals, err)
				continue
			} else {
				t.Errorf("%s: TargetsNeedingBuild(%q): error: got %q, want %q", label, test.goals, err, test.wantErr)
				continue
			}
		}

		// sort so that test is deterministic
		for _, ts := range targetSets {
			sort.Strings(ts)
		}
		if !reflect.DeepEqual(targetSets, test.wantTargetSetsNeedingBuild) {
			t.Errorf("%s: got targetSets needing build %v, want %v", label, targetSets, test.wantTargetSetsNeedingBuild)
		}
	}
}