Open Revlig opened 4 years ago
What kind of challenges are you thinking of? Any examples? Any suggestion on how to test?
We can probably use https://github.com/hashicorp/hcl/tree/hcl2 and compare the result with the expected. I've been using HCL through Terraform, but never used it directly.
I sincerely asked to add it because I am learning about this language and did not find it in codewars. Initially I thought of simple katas based on exercises that I am creating to improve my learning.
I'm following this list of functions: https://www.terraform.io/docs/configuration/functions.html. I'm also thinking about more practical things, like creating modules as well.
[Edit] I just started to find a way to test the kata:
Is more difficult to me because I have 3 childrens, hahaha I will post when find a better way to test.
Maybe this will help: https://github.com/hashicorp/terraform/issues/24269 But...
@kazk Do you need someone to write a test framework? Once I learn more about HCL, I can try write one if you need one.
I don't think we need to write a new test framework.
What I'm currently thinking is to let users submit solution.hcl
and do something like the following to test:
package example_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"log"
"github.com/hashicorp/hcl/v2/hclsimple"
)
type Expected struct {
Field string `hcl:"field"`
}
var _ = Describe("HCL", func() {
It("should have some field with value", func() {
var expected Expected
err := hclsimple.DecodeFile("/workspace/solution.hcl", nil, &expected)
if err != nil {
log.Fatalf("Failed to load: %s", err)
}
Expect(expected.Field).To(Equal("bar"))
})
})
hcl.EvalContext
can be used to pass variables and functions when decoding with DecodeFile
.
@Steffan153 Maybe you can experiment with this idea?
I couldn't get HCL v2 installed, I got this error: C source files not allowed when not using cgo or SWIG: main.c temp.c
:(
BTW, what's up with the Python 3.4.3? I finished all of those kata.
@Steffan153 Did you use Go modules?
# Initialize project
go mod init github.com/Steffan153/example
# Get HCL v2
go get github.com/hashicorp/hcl/v2
# Get test framework and matcher
go get github.com/onsi/ginkgo
go get github.com/onsi/gomega
# Generate tests
ginkgo bootstrap
ginkgo generate
# Add solution.hcl
echo 'field = "bar"' > solution.hcl
# Replace the contents of generated tests and
# run tests
go test
Thanks, I got it working. So this is the basic idea: goproj_test.go:
package goproj_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"log"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsimple"
"github.com/zclconf/go-cty/cty"
)
type Expected struct {
Field int `hcl:"field"`
}
func tester(n int64, exp int) {
var expected Expected
err := hclsimple.DecodeFile("solution.hcl", &hcl.EvalContext{Variables: map[string]cty.Value{"n": cty.NumberIntVal(n)}}, &expected)
if err != nil {
log.Fatalf("Failed to load: %s", err)
}
Expect(expected.Field).To(Equal(exp))
}
var _ = Describe("HCL", func() {
It("basic tests", func() {
tester(5, 6)
tester(7, 8)
})
})
solution.hcl:
field = n + 1
Note that github.com/zclconf/go-cty/cty
needs to be installed too.
Edit: I just made an entire Multiply translation btw.
Any updates on this one?
Please complete the following information:
:+1: reaction might help.