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

Side by Side Diff: vpython/filesystem/testfs/build.go

Issue 2703793003: vpython: Add filesystem operation / test packages. (Closed)
Patch Set: 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
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
12 "github.com/luci/luci-go/vpython/filesystem"
13 )
14
15 // BuildDir is a sentinel value that can be provided to Build as content to
16 // instruct it to make a directory instead of a file.
17 var BuildDir = "\x00"
iannucci 2017/02/21 08:25:08 why not layout := map[string]string { "some/dir
dnj 2017/02/21 18:07:49 Done.
18
19 // Build constructs a filesystem hierarchy given a layout.
20 //
21 // The layouts keys should be ToSlash-style file paths. Its values should be the
22 // content that is written at those paths. Intermediate directories will be
23 // automatically created.
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 content := layout[path]
33 path = filepath.Join(base, filepath.FromSlash(path))
34
35 if content == BuildDir {
36 // Make a directory.
37 if err := filesystem.MakeDirs(path); err != nil {
38 return err
39 }
40 } else {
41 // Make a file.
42 if err := filesystem.MakeDirs(filepath.Dir(path)); err ! = nil {
43 return err
44 }
45 if err := ioutil.WriteFile(path, []byte(content), 0644); err != nil {
46 return err
47 }
48 }
49 }
50 return nil
51 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698