| 1 | package hcl_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/tomwright/dasel/v3/parsing" |
| 8 | "github.com/tomwright/dasel/v3/parsing/hcl" |
| 9 | ) |
| 10 | |
| 11 | type readTestCase struct { |
| 12 | in string |
| 13 | } |
| 14 | |
| 15 | func (tc readTestCase) run(t *testing.T) { |
| 16 | r, err := hcl.HCL.NewReader(parsing.DefaultReaderOptions()) |
| 17 | if err != nil { |
| 18 | t.Fatalf("unexpected error: %v", err) |
| 19 | } |
| 20 | |
| 21 | in := []byte(tc.in) |
| 22 | |
| 23 | got, err := r.Read(in) |
| 24 | if err != nil { |
| 25 | t.Errorf("unexpected error: %v", err) |
| 26 | return |
| 27 | } |
| 28 | |
| 29 | fmt.Println(got) |
| 30 | } |
| 31 | |
| 32 | func TestHclReader_Read(t *testing.T) { |
| 33 | t.Run("document a", readTestCase{ |
| 34 | in: `io_mode = "async" |
| 35 | |
| 36 | service "http" "web_proxy" { |
| 37 | listen_addr = "127.0.0.1:8080" |
| 38 | |
| 39 | process "main" { |
| 40 | command = ["/usr/local/bin/awesome-app", "server"] |
| 41 | } |
| 42 | |
| 43 | process "mgmt" { |
| 44 | command = ["/usr/local/bin/awesome-app", "mgmt"] |
| 45 | } |
| 46 | }`, |
| 47 | }.run) |
| 48 | t.Run("document b", readTestCase{ |
| 49 | in: `resource "aws_instance" "example" { |
| 50 | # (resource configuration omitted for brevity) |
| 51 | |
| 52 | provisioner "local-exec" { |
| 53 | command = "echo 'Hello World' >example.txt" |
| 54 | } |
| 55 | provisioner "file" { |
| 56 | source = "example.txt" |
| 57 | destination = "/tmp/example.txt" |
| 58 | } |
| 59 | provisioner "remote-exec" { |
| 60 | inline = [ |
| 61 | "sudo install-something -f /tmp/example.txt", |
| 62 | ] |
| 63 | } |
| 64 | }`, |
| 65 | }.run) |
| 66 | t.Run("document c", readTestCase{ |
| 67 | in: `image_id = "ami-123" |
| 68 | cluster_min_nodes = 2 |
| 69 | cluster_decimal_nodes = 2.2 |
| 70 | cluster_max_nodes = true |
| 71 | availability_zone_names = [ |
| 72 | "us-east-1a", |
| 73 | "us-west-1c", |
| 74 | ] |
| 75 | docker_ports = [{ |
| 76 | internal = 8300 |
| 77 | external = 8300 |
| 78 | protocol = "tcp" |
| 79 | }, |
| 80 | { |
| 81 | internal = 8301 |
| 82 | external = 8301 |
| 83 | protocol = "tcp" |
| 84 | } |
| 85 | ]`, |
| 86 | }.run) |
| 87 | } |