github.com/solo-io/cue@v0.4.7/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/solo-io/cue/cue" 26 "github.com/solo-io/cue/cue/parser" 27 "github.com/solo-io/cue/internal/task" 28 "github.com/solo-io/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 func TestRead(t *testing.T) { 46 v := parse(t, "tool/file.Read", `{filename: "testdata/input.foo"}`) 47 got, err := (*cmdRead).Run(nil, &task.Context{Obj: v}) 48 if err != nil { 49 t.Fatal(err) 50 } 51 want := map[string]interface{}{"contents": []byte("This is a test.")} 52 if !reflect.DeepEqual(got, want) { 53 t.Errorf("got %v; want %v", got, want) 54 } 55 56 v = parse(t, "tool/file.Read", `{ 57 filename: "testdata/input.foo" 58 contents: string 59 }`) 60 got, err = (*cmdRead).Run(nil, &task.Context{Obj: v}) 61 if err != nil { 62 t.Fatal(err) 63 } 64 want = map[string]interface{}{"contents": "This is a test."} 65 if !reflect.DeepEqual(got, want) { 66 t.Errorf("got %v; want %v", got, want) 67 } 68 } 69 70 func TestAppend(t *testing.T) { 71 f, err := ioutil.TempFile("", "filetest") 72 if err != nil { 73 t.Fatal(err) 74 } 75 name := f.Name() 76 defer os.Remove(name) 77 f.Close() 78 name = filepath.ToSlash(name) 79 80 v := parse(t, "tool/file.Append", fmt.Sprintf(`{ 81 filename: "%s" 82 contents: "This is a test." 83 }`, name)) 84 _, err = (*cmdAppend).Run(nil, &task.Context{Obj: v}) 85 if err != nil { 86 t.Fatal(err) 87 } 88 89 b, err := ioutil.ReadFile(name) 90 if err != nil { 91 t.Fatal(err) 92 } 93 94 if got, want := string(b), "This is a test."; got != want { 95 t.Errorf("got %v; want %v", got, want) 96 } 97 } 98 99 func TestCreate(t *testing.T) { 100 f, err := ioutil.TempFile("", "filetest") 101 if err != nil { 102 t.Fatal(err) 103 } 104 name := f.Name() 105 defer os.Remove(name) 106 f.Close() 107 name = filepath.ToSlash(name) 108 109 v := parse(t, "tool/file.Create", fmt.Sprintf(`{ 110 filename: "%s" 111 contents: "This is a test." 112 }`, name)) 113 _, err = (*cmdCreate).Run(nil, &task.Context{Obj: v}) 114 if err != nil { 115 t.Fatal(err) 116 } 117 118 b, err := ioutil.ReadFile(name) 119 if err != nil { 120 t.Fatal(err) 121 } 122 123 if got, want := string(b), "This is a test."; got != want { 124 t.Errorf("got %v; want %v", got, want) 125 } 126 } 127 128 func TestGlob(t *testing.T) { 129 v := parse(t, "tool/file.Glob", fmt.Sprintf(`{ 130 glob: "testdata/input.*" 131 }`)) 132 got, err := (*cmdGlob).Run(nil, &task.Context{Obj: v}) 133 if err != nil { 134 t.Fatal(err) 135 } 136 if want := map[string]interface{}{"files": []string{"testdata/input.foo"}}; !reflect.DeepEqual(got, want) { 137 t.Errorf("got %v; want %v", got, want) 138 } 139 }