| OLD | NEW |
| (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 } |
| OLD | NEW |