Commit Graph

34 Commits

Author SHA1 Message Date
Martin Atkins 7887810587 specsuite: Fix some regressions
Lately we've made some changes that have affected the behavior of the
specsuite tests, causing them to fail. Much of this was caused by changes
to the harness itself (based on hcldec), although one break in particular
here was an intentional change to the implementation of modulo in
upstream cty to make it produce a more accurate result when used with
a fractional divisor.
2021-02-23 09:05:19 -08:00
Masayuki Morita cec773f974 Fix various stale links to spec.md
These resulted from repository reorganization in preparation for the 2.0.0 release.
2020-01-09 11:02:20 -08:00
Martin Atkins a21a3f1908 Fix import ordering from the move from hcl2 repository
When we did the automatic rewriting from the hashicorp/hcl2 import paths
to hashicorp/hcl/v2 import paths, our automatic rewrite script did not
run "go fmt" afterwards and so left some imports misordered as a result
of the slightly-differently-shaped directory structure in the new
repository.
2019-12-11 07:34:30 -08:00
Martin Atkins 791068cd07 cmd/hclspecsuite: run hcldec with --keep-nulls
This gives us a more precise picture of the result, which is helpful to
ensure our tests are not "cheating".
2019-10-01 15:59:10 -07:00
Martin Atkins c366498686 cmd/hcldec: Allow overriding the removal of nulls
We remove properties whose values are null by default in order to produce
output that is more convenient to consume in the common case.

However, sometimes those nulls are significant, so we'll allow the user
to opt in to retaining them, at the expense of producing a result that is
more noisy if the spec contains lots of optional attributes that are not
set.
2019-10-01 15:59:10 -07:00
Martin Atkins 6c4344623b Unfold the "hcl" directory up into the root
The main HCL package is more visible this way, and so it's easier than
having to pick it out from dozens of other package directories.
2019-09-09 16:08:19 -07:00
Martin Atkins 3327dee567 Change module path to github.com/hashicorp/hcl/v2
This is in preparation for the first v2 release from the main HCL
repository.
2019-09-09 15:46:40 -07:00
Buck Doyle 6b2febac15 Remove incorrect quotes around types in examples (#119)
When I tried a spec with quoted types, I got this error:
`A type is required, not string.`

Later in the document, the types don’t have quotes, so I figured that’s what’s expected.
2019-07-22 15:49:22 -04:00
Martin Atkins 2934d2f033 cmd/hclspecsuite: Generate correct ranges in diagnostic-diagnostics
We were taking a pointer to a for loop iterator variable and thus
capturing the final iteration value rather than each one separately. By
using the .Ptr() method instead, we force a copy of the range which we
then take a pointer to.
2018-12-14 11:33:28 -08:00
Martin Atkins b82170e941 hcldec: Handle or forbid cty.DynamicPseudoType attributes in nested blocks
Our BlockList, BlockSet, and BlockMap specs all produce cty collection
values, which require all elements to have a homogeneous type. If the
nested spec contained an attribute of type cty.DynamicPseudoType, that
would create the risk of each element having a different type, which would
previously have caused decoding to panic.

Now we either handle this during decode (BlockList, BlockSet) or forbid
it outright (BlockMap) to prevent that crash. BlockMap could _potentially_
also handle this during decode, but that would require a more significant
reorganization of its implementation than I want to take on right now,
and decoding dynamically-typed values inside collections is an edge case
anyway.
2018-08-22 11:52:40 -07:00
Martin Atkins ef5c50bb09 hcl/spectests: run the spec testsuite as part of "go test"
Although the spec testsuite and associated harness is designed to be
usable by other implementations of HCL not written in Go, it's convenient
to run it as part of our own "go test" test suite here so there isn't
an additional thing to run on each change.

To achieve this, the new package hcl/spectests will build both hcldec and
hclspecsuite from latest source and then run the latter to execute the
test suite, capturing the output and converting it (sloppily) into
testing.T method calls to produce something vaguely reasonable.

Other than the small amount of "parsing" to make it look in the output
like a normal Go test, there's nothing special going on here and so it's
still valid to run the spec suite manually with a build of hcldec from
this codebase, which should produce the same result.
2018-08-12 18:22:10 -07:00
Martin Atkins 74466f4d35 cmd/hclspecsuite: show source snippets for errors in .hcldec files 2018-08-12 10:29:13 -07:00
Martin Atkins a5c0f7fdcc cmd/hclspecsuite: Check for expected diagnostics
When a test file declares one or more expected diagnostics, we check those
instead of checking the result value. The severities and source ranges
must match.

We don't test the error messages themselves because they are not part of
the specification and may vary between implementations or, in future, be
translated into other languages.
2018-08-12 10:08:27 -07:00
Martin Atkins 767fb36174 cmd/hclspecsuite: Check for traversals when requested 2018-08-12 09:31:28 -07:00
Martin Atkins 95b1859585 cmd/hclspecsuite: Decode expected traversals and diagnostics
These are not used by the test harness yet, but they can now be decoded
from a test spec file.
2018-08-11 20:21:32 -07:00
Martin Atkins 48039c0368 cmd/hclspecsuite: basic runner functionality for successful cases
The harness can now run tests that decode successfully and compare the
result with a given value. Further work is required in later commits to
deal with other cases, such as tests that intentionally produce errors.
2018-08-10 08:49:43 -07:00
Martin Atkins 0956c193b7 specsuite: Start of the harness for the specification test suite 2018-08-09 19:29:32 -07:00
Martin Atkins 6743a2254b cmd/hcldec: opt-in JSON-formatted diagnostics
By default we generate human-readable diagnostics on the assumption that
the caller is a simple program that is capturing stdin via a pipe and
letting stderr go to the terminal.

More sophisticated callers may wish to analyze the diagnostics themselves
and perhaps present them in a different way, such as via a GUI.
2018-08-09 18:10:14 -07:00
Martin Atkins 609cc35d49 cmd/hcldec: --var-refs option
This option skips the usual decoding step and instead prints out a JSON-
formatted list of the variables that are referenced by the configuration.

In simple cases this is not required, but for more complex use-cases it
can be useful to first analyze the input to see which variables need to
be in the scope, then construct a suitable set of variables before finally
decoding the input. For example, some of the variable values may be
expensive to produce.
2018-08-09 18:10:14 -07:00
Martin Atkins bb724af7fd hcldec: BlockAttrsSpec spec type
This is the hcldec interface to Body.JustAttributes, producing a map whose
keys are the child attribute names and whose values are the results of
evaluating those expressions.

We can't just expose a JustAttributes-style spec directly here because
it's not really compatible with how hcldec thinks about things, but we
can expose a spec that decodes a specific child block because that can
then compose properly with other specs at the same level without
interfering with their operation.

The primary use for this is to allow the use of the block syntax to define
a map:

    dynamic_stuff {
      foo = "bar"
    }

JustAttributes is normally used in static analysis situations such as
enumerating the contents of a block to decide what to include in the
final EvalContext. That's not really possible with the hcldec model
because both structural decoding and expression evaluation happen
together. Therefore the use of this is pretty limited: it's useful if you
want to be compatible with an existing format based on legacy HCL where a
map was conventionally defined using block syntax, relying on the fact
that HCL did not make a strong distinction between attribute and block
syntax.
2018-08-09 16:53:16 -07:00
Martin Atkins 18a92d222b ext/userfunc: use bare identifiers for param names
Now that we have the necessary functions to deal with this in the
low-level HCL API, it's more intuitive to use bare identifiers for these
parameter names. This reinforces the idea that they are symbols being
defined rather than arbitrary string expressions.
2018-02-04 11:20:42 -08:00
Martin Atkins 2ddf8b4b8c cmd/hcldec: allow spec file to define variables and functions
The spec file can now additionally define default variables and functions
for the eval context used to evaluate the input file.
2018-02-04 11:05:23 -08:00
Martin Atkins 6c3ae68a0e cmd/hcldec: make cty stdlib functions available to specs
In a few specific portions of the spec format it's convenient to have
access to some of the functions defined in the cty stdlib. Here we allow
them to be used when constructing the value for a "literal" spec and in
the result expression for a "transform" spec.
2018-02-04 10:33:35 -08:00
Martin Atkins 1ba92ee170 cmd/hcldec: "transform" spec type
This new spec type allows evaluating an arbitrary expression on the
result of a nested spec, for situations where the a value must be
transformed in some way.
2018-02-04 09:59:20 -08:00
Martin Atkins f65a097d17 cmd/hcldec: decode "array" blocks
These were missed on the previous pass, causing a disagreement with the
documentation.
2018-02-04 09:45:28 -08:00
Martin Atkins ee147d9ee6 cmd/hcldec: Command-line tool for converting HCL config to JSON
This is essentially a CLI wrapper around the hcldec package, accepting a
decoding specification via a HCL-based language and using it to translate
input HCL files into JSON values while performing basic structural and
type validation of the input files.
2018-02-03 15:37:11 -08:00
Martin Atkins a59f60c1db hclfmt: Have this tool call itself by the correct name
Its usage output was still reporting "zclfmt", which is a remnant of the
experimental project that HCL was forked from.
2018-01-27 11:03:05 -08:00
Martin Atkins b8b5e0be6d hclparse: rename ParseZCL to ParseHCL 2017-09-11 17:22:10 -07:00
Martin Atkins 708abb8c97 Move the zcl package and its two parsing subpackages to "hcl" names
This is a super-invasive update since the "zcl" package in particular
is referenced all over.

There are probably still a few zcl references hanging around in comments,
etc but this takes care of most of it.
2017-09-11 16:40:37 -07:00
Martin Atkins 0dc3a6015c Rename the ancillary packages from "zcl" to "hcl".
The main "zcl" package requires a bit more care because of how many
callers it has and because of its two subpackages, so we'll take care
of that one separately.
2017-09-11 16:00:31 -07:00
Martin Atkins c3ca111fff Adjust import paths for hashicorp/hcl2 repo
This begins 'the great fork' of zcl to HCL.
2017-09-11 15:38:42 -07:00
Martin Atkins 2c9302b699 cmd/zclfmt: skip formatting a file only if it has errors
Previously we were erroneously skipping formatting it whenever check mode
was enabled, regardless of errors.
2017-06-24 09:13:48 -07:00
Martin Atkins d1fb42746b cmd/zclfmt: new options for checking validity of input files 2017-06-24 09:02:30 -07:00
Martin Atkins aba54359ba cmd/zclfmt: zcl native syntax pretty-printer
This applies the simple native syntax reformatting function to one or
more files. It does not support JSON or any other syntax.

Calling applications might provide their own versions of this that e.g.
can format an entire directory by matching on filename patterns, but
this serves as an example and a utility for single files.
2017-06-24 09:02:11 -07:00