aboutsummaryrefslogtreecommitdiff
path: root/lib/test/data.go
diff options
context:
space:
mode:
Diffstat (limited to 'lib/test/data.go')
-rw-r--r--lib/test/data.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/test/data.go b/lib/test/data.go
index 281de010..d113e87c 100644
--- a/lib/test/data.go
+++ b/lib/test/data.go
@@ -207,6 +207,46 @@ func newData(name string) (data *Data) {
return data
}
+// ExtractInput given the path to directory `destDir`, create all of the
+// [test.Data.Input] with key as its file name.
+//
+// If the input name contains "/", the path before the base name will be
+// created along with its parent as long as it is under the `destDir`.
+//
+// For example, given input name "a/b/c.txt", it will create path "a/b/"
+// inside `destDir` first, followed by file "c.txt" inside that path.
+func (data *Data) ExtractInput(destDir string) (err error) {
+ var (
+ logp = `ExtractInput`
+ name string
+ dir string
+ base string
+ path string
+ content []byte
+ )
+ for name, content = range data.Input {
+ dir = filepath.Dir(name)
+ base = filepath.Base(name)
+ path = filepath.Join(destDir, dir)
+
+ if strings.HasPrefix(path, destDir) && dir != `.` && dir != `/` {
+ err = os.MkdirAll(path, 0700)
+ if err != nil {
+ return fmt.Errorf(`%s: %w`, logp, err)
+ }
+ path = filepath.Join(path, base)
+ } else {
+ path = filepath.Join(destDir, base)
+ }
+
+ err = os.WriteFile(path, content, 0600)
+ if err != nil {
+ return fmt.Errorf(`%s: %w`, logp, err)
+ }
+ }
+ return nil
+}
+
func (data *Data) parse(content []byte) (err error) {
const (
stateFlag int = iota