aboutsummaryrefslogtreecommitdiff
path: root/lib/test/data.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2025-12-30 20:01:24 +0700
committerShulhan <ms@kilabit.info>2026-01-03 04:42:41 +0700
commit2d18ac7fd9f8ddc92ddfb7de772adea8468ac3ea (patch)
tree7644ffb9d48849a314fd24ac805c90c8a801d0e3 /lib/test/data.go
parent3055ad4365d5625f5bebb18cb5a4aec00f09fea8 (diff)
downloadpakakeh.go-2d18ac7fd9f8ddc92ddfb7de772adea8468ac3ea.tar.xz
lib/test: implement method ExtractInput on Data
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.
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