diff options
Diffstat (limited to 'lib/test')
| -rw-r--r-- | lib/test/data.go | 40 | ||||
| -rw-r--r-- | lib/test/data_example_test.go | 69 |
2 files changed, 109 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 diff --git a/lib/test/data_example_test.go b/lib/test/data_example_test.go new file mode 100644 index 00000000..b544dacd --- /dev/null +++ b/lib/test/data_example_test.go @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: BSD-3-Clause +// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> + +package test_test + +import ( + "fmt" + "log" + "os" + "path/filepath" + + "git.sr.ht/~shulhan/pakakeh.go/lib/test" +) + +func ExampleData_ExtractInput() { + var tempDir string + var err error + tempDir, err = os.MkdirTemp(``, ``) + if err != nil { + log.Fatal(err) + } + defer os.RemoveAll(tempDir) + + var data = &test.Data{ + Input: map[string][]byte{ + `dir/a.txt`: []byte(`Content of dir/a.txt.`), + `dir/sub/b.txt`: []byte(`Content of dir/sub/b.txt.`), + `c.txt`: []byte(`Content of c.txt.`), + `dir/../../d.txt`: []byte(`Content of d.txt.`), + }, + } + + // The ExtractInput will create the following directory structures, + // including their files, + // + // ├── c.txt + // ├── dir + // │ ├── a.txt + // │ └── sub + // │ └── b.txt + // └── d.txt + // + err = data.ExtractInput(tempDir) + if err != nil { + log.Fatal(err) + } + + var listExtractedInput = []string{ + filepath.Join(tempDir, `dir/a.txt`), + filepath.Join(tempDir, `dir/sub/b.txt`), + filepath.Join(tempDir, `c.txt`), + // Since the path of "dir/../../d.txt" is outside of the + // tempDir, the file will be created on root of tempDir. + filepath.Join(tempDir, `d.txt`), + } + var got []byte + for _, path := range listExtractedInput { + got, err = os.ReadFile(path) + if err != nil { + log.Fatal(err) + } + fmt.Printf("%s\n", got) + } + // Output: + // Content of dir/a.txt. + // Content of dir/sub/b.txt. + // Content of c.txt. + // Content of d.txt. +} |
