github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/pkg/tool/file/file_test.go (about)

     1  // Copyright 2019 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package file
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"path/filepath"
    22  	"reflect"
    23  	"testing"
    24  
    25  	"github.com/joomcode/cue/cue"
    26  	"github.com/joomcode/cue/cue/parser"
    27  	"github.com/joomcode/cue/internal/task"
    28  	"github.com/joomcode/cue/internal/value"
    29  )
    30  
    31  func parse(t *testing.T, kind, expr string) cue.Value {
    32  	t.Helper()
    33  
    34  	x, err := parser.ParseExpr("test", expr)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	var r cue.Runtime
    39  	i, err := r.CompileExpr(x)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	return value.UnifyBuiltin(i.Value(), kind)
    44  }
    45  
    46  func TestRead(t *testing.T) {
    47  	v := parse(t, "tool/file.Read", `{filename: "testdata/input.foo"}`)
    48  	got, err := (*cmdRead).Run(nil, &task.Context{Obj: v})
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	want := map[string]interface{}{"contents": []byte("This is a test.")}
    53  	if !reflect.DeepEqual(got, want) {
    54  		t.Errorf("got %v; want %v", got, want)
    55  	}
    56  
    57  	v = parse(t, "tool/file.Read", `{
    58  		filename: "testdata/input.foo"
    59  		contents: string
    60  	}`)
    61  	got, err = (*cmdRead).Run(nil, &task.Context{Obj: v})
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	want = map[string]interface{}{"contents": "This is a test."}
    66  	if !reflect.DeepEqual(got, want) {
    67  		t.Errorf("got %v; want %v", got, want)
    68  	}
    69  }
    70  
    71  func TestAppend(t *testing.T) {
    72  	f, err := ioutil.TempFile("", "filetest")
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	name := f.Name()
    77  	defer os.Remove(name)
    78  	f.Close()
    79  	name = filepath.ToSlash(name)
    80  
    81  	v := parse(t, "tool/file.Append", fmt.Sprintf(`{
    82  		filename: "%s"
    83  		contents: "This is a test."
    84  	}`, name))
    85  	_, err = (*cmdAppend).Run(nil, &task.Context{Obj: v})
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	b, err := ioutil.ReadFile(name)
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	if got, want := string(b), "This is a test."; got != want {
    96  		t.Errorf("got %v; want %v", got, want)
    97  	}
    98  }
    99  
   100  func TestCreate(t *testing.T) {
   101  	f, err := ioutil.TempFile("", "filetest")
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  	name := f.Name()
   106  	defer os.Remove(name)
   107  	f.Close()
   108  	name = filepath.ToSlash(name)
   109  
   110  	v := parse(t, "tool/file.Create", fmt.Sprintf(`{
   111  		filename: "%s"
   112  		contents: "This is a test."
   113  	}`, name))
   114  	_, err = (*cmdCreate).Run(nil, &task.Context{Obj: v})
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  
   119  	b, err := ioutil.ReadFile(name)
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  
   124  	if got, want := string(b), "This is a test."; got != want {
   125  		t.Errorf("got %v; want %v", got, want)
   126  	}
   127  }
   128  
   129  func TestGlob(t *testing.T) {
   130  	v := parse(t, "tool/file.Glob", fmt.Sprintf(`{
   131  		glob: "testdata/input.*"
   132  	}`))
   133  	got, err := (*cmdGlob).Run(nil, &task.Context{Obj: v})
   134  	if err != nil {
   135  		t.Fatal(err)
   136  	}
   137  	if want := map[string]interface{}{"files": []string{"testdata/input.foo"}}; !reflect.DeepEqual(got, want) {
   138  		t.Errorf("got %v; want %v", got, want)
   139  	}
   140  }
   141  
   142  func TestMkdir(t *testing.T) {
   143  	baseDir, err := os.MkdirTemp("", "")
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  	defer os.RemoveAll(baseDir)
   148  
   149  	// simple dir creation
   150  	d1 := filepath.Join(baseDir, "foo")
   151  	v := parse(t, "tool/file.Mkdir", fmt.Sprintf(`{path: #"%s"#}`, d1))
   152  	_, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v})
   153  	if err != nil {
   154  		t.Fatal(err)
   155  	}
   156  	fi1, err := os.Stat(d1)
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  	if !fi1.IsDir() {
   161  		t.Fatal("not a directory")
   162  	}
   163  
   164  	// dir already exists
   165  	v = parse(t, "tool/file.Mkdir", fmt.Sprintf(`{path: #"%s"#}`, d1))
   166  	_, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v})
   167  	if err != nil {
   168  		t.Fatal(err)
   169  	}
   170  
   171  	// create parents
   172  	// set permissions
   173  	d2 := filepath.Join(baseDir, "bar/x")
   174  	v = parse(t, "tool/file.MkdirAll", fmt.Sprintf(`{path: #"%s"#, permissions: 0o700}`, d2))
   175  	_, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v})
   176  	if err != nil {
   177  		t.Fatal(err)
   178  	}
   179  	fi2, err := os.Stat(d2)
   180  	if err != nil {
   181  		t.Fatal(err)
   182  	}
   183  	if !fi2.IsDir() {
   184  		t.Fatal("not a directory")
   185  	}
   186  
   187  	// file at same path
   188  	f, err := ioutil.TempFile(baseDir, "")
   189  	if err != nil {
   190  		t.Fatal(err)
   191  	}
   192  	v = parse(t, "tool/file.Mkdir", fmt.Sprintf(`{path: #"%s"#}`, f.Name()))
   193  	_, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v})
   194  	if err == nil {
   195  		t.Fatal("should not create directory at existing filepath")
   196  	}
   197  }
   198  
   199  func TestMkdirTemp(t *testing.T) {
   200  	// create temp dir
   201  	v := parse(t, "tool/file.MkdirTemp", "{}")
   202  	r, err := (*cmdMkdirTemp).Run(nil, &task.Context{Obj: v})
   203  	if err != nil {
   204  		t.Fatal(err)
   205  	}
   206  	if _, exists := r.(map[string]interface{})["path"]; !exists {
   207  		t.Fatal("no directory path returned")
   208  	}
   209  	path := r.(map[string]interface{})["path"].(string)
   210  	defer os.RemoveAll(path)
   211  	fi, err := os.Stat(path)
   212  	if err != nil {
   213  		t.Fatal(err)
   214  	}
   215  	if !fi.IsDir() {
   216  		t.Fatal("not a directory")
   217  	}
   218  
   219  	// removes temp dir
   220  	v2 := parse(t, "tool/file.RemoveAll", fmt.Sprintf(`{path: #"%s"#}`, path))
   221  	_, err = (*cmdRemoveAll).Run(nil, &task.Context{Obj: v2})
   222  	if err != nil {
   223  		t.Fatal(err)
   224  	}
   225  	_, err = os.Stat(path)
   226  	if err == nil {
   227  		t.Fatal(err)
   228  	}
   229  
   230  }