blob: e82081fc5d16b793dfb85577c431ef5a1c8607eb [file] [log] [blame]
Marcel van Lohuizen83b33c02019-04-26 22:42:16 +02001// 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
15package file
16
17import (
18 "fmt"
19 "io/ioutil"
20 "os"
Marcel van Lohuizena1d3b2d2019-06-18 14:32:36 +020021 "path/filepath"
Marcel van Lohuizen83b33c02019-04-26 22:42:16 +020022 "reflect"
23 "testing"
24
25 "cuelang.org/go/cue"
26 "cuelang.org/go/cue/parser"
Marcel van Lohuizen83b33c02019-04-26 22:42:16 +020027 "cuelang.org/go/internal"
28)
29
30func parse(t *testing.T, kind, expr string) cue.Value {
31 t.Helper()
Marcel van Lohuizen83b33c02019-04-26 22:42:16 +020032
Marcel van Lohuizen3c7c40b2019-05-22 11:31:27 -040033 x, err := parser.ParseExpr("test", expr)
Marcel van Lohuizen83b33c02019-04-26 22:42:16 +020034 if err != nil {
35 t.Fatal(err)
36 }
Marcel van Lohuizencc9ce432019-05-23 13:48:44 -040037 var r cue.Runtime
38 i, err := r.FromExpr(x)
Marcel van Lohuizen83b33c02019-04-26 22:42:16 +020039 if err != nil {
40 t.Fatal(err)
41 }
42 return internal.UnifyBuiltin(i.Value(), kind).(cue.Value)
43}
44func TestRead(t *testing.T) {
45 v := parse(t, "tool/file.Read", `{filename: "testdata/input.foo"}`)
46 got, err := (*cmdRead).Run(nil, nil, v)
47 if err != nil {
48 t.Fatal(err)
49 }
50 want := map[string]interface{}{"contents": []byte("This is a test.")}
51 if !reflect.DeepEqual(got, want) {
52 t.Errorf("got %v; want %v", got, want)
53 }
54
55 v = parse(t, "tool/file.Read", `{
56 filename: "testdata/input.foo"
57 contents: string
58 }`)
59 got, err = (*cmdRead).Run(nil, nil, v)
60 if err != nil {
61 t.Fatal(err)
62 }
63 want = map[string]interface{}{"contents": "This is a test."}
64 if !reflect.DeepEqual(got, want) {
65 t.Errorf("got %v; want %v", got, want)
66 }
67}
68
69func TestAppend(t *testing.T) {
70 f, err := ioutil.TempFile("", "filetest")
71 if err != nil {
72 t.Fatal(err)
73 }
74 name := f.Name()
75 defer os.Remove(name)
76 f.Close()
Marcel van Lohuizena1d3b2d2019-06-18 14:32:36 +020077 name = filepath.ToSlash(name)
Marcel van Lohuizen83b33c02019-04-26 22:42:16 +020078
79 v := parse(t, "tool/file.Append", fmt.Sprintf(`{
80 filename: "%s"
81 contents: "This is a test."
82 }`, name))
83 _, err = (*cmdAppend).Run(nil, nil, v)
84 if err != nil {
85 t.Fatal(err)
86 }
87
88 b, err := ioutil.ReadFile(name)
89 if err != nil {
90 t.Fatal(err)
91 }
92
93 if got, want := string(b), "This is a test."; got != want {
94 t.Errorf("got %v; want %v", got, want)
95 }
96}
97
98func TestCreate(t *testing.T) {
99 f, err := ioutil.TempFile("", "filetest")
100 if err != nil {
101 t.Fatal(err)
102 }
103 name := f.Name()
104 defer os.Remove(name)
105 f.Close()
Marcel van Lohuizena1d3b2d2019-06-18 14:32:36 +0200106 name = filepath.ToSlash(name)
Marcel van Lohuizen83b33c02019-04-26 22:42:16 +0200107
108 v := parse(t, "tool/file.Create", fmt.Sprintf(`{
109 filename: "%s"
110 contents: "This is a test."
111 }`, name))
112 _, err = (*cmdCreate).Run(nil, nil, v)
113 if err != nil {
114 t.Fatal(err)
115 }
116
117 b, err := ioutil.ReadFile(name)
118 if err != nil {
119 t.Fatal(err)
120 }
121
122 if got, want := string(b), "This is a test."; got != want {
123 t.Errorf("got %v; want %v", got, want)
124 }
125}
126
127func TestGlob(t *testing.T) {
128 v := parse(t, "tool/file.Glob", fmt.Sprintf(`{
129 glob: "testdata/input.*"
130 }`))
131 got, err := (*cmdGlob).Run(nil, nil, v)
132 if err != nil {
133 t.Fatal(err)
134 }
135
136 if want := []string{"testdata/input.foo"}; !reflect.DeepEqual(got, want) {
137 t.Errorf("got %v; want %v", got, want)
138 }
139}