Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Unified Diff: vpython/filesystem/testfs/build.go

Issue 2703793003: vpython: Add filesystem operation / test packages. (Closed)
Patch Set: test now path Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « vpython/filesystem/remove_all.go ('k') | vpython/filesystem/testfs/tempdir.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: vpython/filesystem/testfs/build.go
diff --git a/vpython/filesystem/testfs/build.go b/vpython/filesystem/testfs/build.go
new file mode 100644
index 0000000000000000000000000000000000000000..facf2720ef77408d6d73c342e71dd6f9d2ba657d
--- /dev/null
+++ b/vpython/filesystem/testfs/build.go
@@ -0,0 +1,55 @@
+// Copyright 2017 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package testfs
+
+import (
+ "io/ioutil"
+ "path/filepath"
+ "sort"
+ "strings"
+
+ "github.com/luci/luci-go/vpython/filesystem"
+)
+
+// Build constructs a filesystem hierarchy given a layout.
+//
+// The layouts keys should be ToSlash-style file paths. Its values should be the
+// content that is written at those paths. Intermediate directories will be
+// automatically created.
+//
+// To create a directory, end its path with a "/". In this case, the content
+// will be ignored.
+func Build(base string, layout map[string]string) error {
+ keys := make([]string, 0, len(layout))
+ for k := range layout {
+ keys = append(keys, k)
+ }
+ sort.Strings(keys)
+
+ for _, path := range keys {
+ makeDir := strings.HasSuffix(path, "/")
+ content := layout[path]
+
+ // Normalize "path" to the current OS.
+ path = filepath.Join(base, filepath.FromSlash(path))
+
+ if makeDir {
+ // Make a directory.
+ if err := filesystem.MakeDirs(path); err != nil {
+ return err
+ }
+ } else {
+ // Make a file.
+
+ if err := filesystem.MakeDirs(filepath.Dir(path)); err != nil {
+ return err
+ }
+ if err := ioutil.WriteFile(path, []byte(content), 0644); err != nil {
+ return err
+ }
+ }
+ }
+ return nil
+}
« no previous file with comments | « vpython/filesystem/remove_all.go ('k') | vpython/filesystem/testfs/tempdir.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698