Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 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" |
Marcel van Lohuizen | a1d3b2d | 2019-06-18 14:32:36 +0200 | [diff] [blame] | 21 | "path/filepath" |
Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 22 | "reflect" |
| 23 | "testing" |
| 24 | |
| 25 | "cuelang.org/go/cue" |
| 26 | "cuelang.org/go/cue/parser" |
Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 27 | "cuelang.org/go/internal" |
| 28 | ) |
| 29 | |
| 30 | func parse(t *testing.T, kind, expr string) cue.Value { |
| 31 | t.Helper() |
Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 32 | |
Marcel van Lohuizen | 3c7c40b | 2019-05-22 11:31:27 -0400 | [diff] [blame] | 33 | x, err := parser.ParseExpr("test", expr) |
Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
Marcel van Lohuizen | cc9ce43 | 2019-05-23 13:48:44 -0400 | [diff] [blame] | 37 | var r cue.Runtime |
| 38 | i, err := r.FromExpr(x) |
Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | return internal.UnifyBuiltin(i.Value(), kind).(cue.Value) |
| 43 | } |
| 44 | func 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 | |
| 69 | func 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 Lohuizen | a1d3b2d | 2019-06-18 14:32:36 +0200 | [diff] [blame] | 77 | name = filepath.ToSlash(name) |
Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 78 | |
| 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 | |
| 98 | func 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 Lohuizen | a1d3b2d | 2019-06-18 14:32:36 +0200 | [diff] [blame] | 106 | name = filepath.ToSlash(name) |
Marcel van Lohuizen | 83b33c0 | 2019-04-26 22:42:16 +0200 | [diff] [blame] | 107 | |
| 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 | |
| 127 | func 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 | } |