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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « vpython/filesystem/remove_all.go ('k') | vpython/filesystem/testfs/tempdir.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package testfs
6
7 import (
8 "io/ioutil"
9 "path/filepath"
10 "sort"
11 "strings"
12
13 "github.com/luci/luci-go/vpython/filesystem"
14 )
15
16 // Build constructs a filesystem hierarchy given a layout.
17 //
18 // The layouts keys should be ToSlash-style file paths. Its values should be the
19 // content that is written at those paths. Intermediate directories will be
20 // automatically created.
21 //
22 // To create a directory, end its path with a "/". In this case, the content
23 // will be ignored.
24 func Build(base string, layout map[string]string) error {
25 keys := make([]string, 0, len(layout))
26 for k := range layout {
27 keys = append(keys, k)
28 }
29 sort.Strings(keys)
30
31 for _, path := range keys {
32 makeDir := strings.HasSuffix(path, "/")
33 content := layout[path]
34
35 // Normalize "path" to the current OS.
36 path = filepath.Join(base, filepath.FromSlash(path))
37
38 if makeDir {
39 // Make a directory.
40 if err := filesystem.MakeDirs(path); err != nil {
41 return err
42 }
43 } else {
44 // Make a file.
45
46 if err := filesystem.MakeDirs(filepath.Dir(path)); err ! = nil {
47 return err
48 }
49 if err := ioutil.WriteFile(path, []byte(content), 0644); err != nil {
50 return err
51 }
52 }
53 }
54 return nil
55 }
OLDNEW
« 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