This change implements dependency versions, including semantic analysis, per the
checkin 83030685c3
.
There's quite a bit in here but at a top-level this parses and validates dependency
references of the form
[[proto://]base.url]namespace/.../name[@version]
and verifies that the components are correct, as well as binding them to symbols.
These references can appear in two places at the moment:
* Service types.
* Cluster dependencies.
As part of this change, a number of supporting changes have been made:
* Parse Workspaces using a full-blown parser, parser analysis, and semantic analysis.
This allows us to share logic around the validation of common AST types. This also
moves some of the logic around loading workspace.yaml files back to the parser, where
it can be unified with the way we load Mu.yaml files.
* New ast.Version and ast.VersionSpec types. The former represents a precise version
-- either a specific semantic version or a short or long Git SHA hash -- and the
latter represents a range -- either a Version, "latest", or a semantic range.
* New ast.Ref and ast.RefParts types. The former is an unparsed string that is
thought to contain a Ref, while the latter is a validated Ref that has been parsed
into its components (Proto, Base, Name, and Version).
* Added some type assertions to ensure certain structs implement certain interfaces,
to speed up finding errors. (And remove the coercions that zero-fill vtbl slots.)
* Be consistent about prefixing error types with Error or Warning.
* Organize the core compiler driver's logic into three methods, FE, sema, and BE.
* A bunch of tests for some of the above ... more to come in an upcoming change.
103 lines
3.4 KiB
Go
103 lines
3.4 KiB
Go
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
package compiler
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/marapongo/mu/pkg/compiler/backends"
|
|
"github.com/marapongo/mu/pkg/compiler/backends/clouds"
|
|
"github.com/marapongo/mu/pkg/diag"
|
|
"github.com/marapongo/mu/pkg/errors"
|
|
)
|
|
|
|
func TestBadMissingMufile(t *testing.T) {
|
|
sink := buildNoCodegen("testdata", "compiler", "bad__missing_mufile")
|
|
|
|
// Check that the compiler complained about a missing Mufile.
|
|
d := errors.ErrorMissingMufile
|
|
assert.Equal(t, 1, sink.Errors(), "expected a single error")
|
|
assert.Equal(t,
|
|
fmt.Sprintf("%v: %v%v: %v\n",
|
|
diag.DefaultSinkErrorPrefix, diag.DefaultSinkIDPrefix, d.ID, fmt.Sprintf(d.Message, sink.Pwd)),
|
|
sink.ErrorMsgs()[0])
|
|
}
|
|
|
|
func TestBadMufileCasing(t *testing.T) {
|
|
sink := buildNoCodegen("testdata", "compiler", "bad__mufile_casing")
|
|
|
|
// Check that the compiler warned about a bad Mufile casing (mu.yaml).
|
|
d := errors.WarningIllegalMufileCasing
|
|
assert.Equal(t, 1, sink.Warnings(), "expected a single warning")
|
|
assert.Equal(t,
|
|
fmt.Sprintf("%v: %v%v: %v: %v\n",
|
|
diag.DefaultSinkWarningPrefix, diag.DefaultSinkIDPrefix, d.ID, "mu.yaml", d.Message),
|
|
sink.WarningMsgs()[0])
|
|
}
|
|
|
|
func TestBadMufileExt1(t *testing.T) {
|
|
sink := buildNoCodegen("testdata", "compiler", "bad__mufile_ext__1")
|
|
|
|
// Check that the compiler warned about a bad Mufile extension (none).
|
|
d := errors.WarningIllegalMufileExt
|
|
assert.Equal(t, 1, sink.Warnings(), "expected a single warning")
|
|
assert.Equal(t,
|
|
fmt.Sprintf("%v: %v%v: %v: %v\n",
|
|
diag.DefaultSinkWarningPrefix, diag.DefaultSinkIDPrefix, d.ID, "Mu", fmt.Sprintf(d.Message, "")),
|
|
sink.WarningMsgs()[0])
|
|
}
|
|
|
|
func TestBadMufileExt2(t *testing.T) {
|
|
sink := buildNoCodegen("testdata", "compiler", "bad__mufile_ext__2")
|
|
|
|
// Check that the compiler warned about a bad Mufile extension (".txt").
|
|
d := errors.WarningIllegalMufileExt
|
|
assert.Equal(t, 1, sink.Warnings(), "expected a single warning")
|
|
assert.Equal(t,
|
|
fmt.Sprintf("%v: %v%v: %v: %v\n",
|
|
diag.DefaultSinkWarningPrefix, diag.DefaultSinkIDPrefix, d.ID, "Mu.txt", fmt.Sprintf(d.Message, ".txt")),
|
|
sink.WarningMsgs()[0])
|
|
}
|
|
|
|
func TestMissingTarget(t *testing.T) {
|
|
mufile := []byte("name: notarget\n" +
|
|
"abstract: true\n")
|
|
|
|
// Check that the compiler issued an error due to missing cloud targets.
|
|
sink := buildFile(Options{}, mufile, ".yaml")
|
|
d := errors.ErrorMissingTarget
|
|
assert.Equal(t, 1, sink.Errors(), "expected a single error")
|
|
assert.Equal(t,
|
|
fmt.Sprintf("%v: %v%v: %v: %v\n",
|
|
diag.DefaultSinkErrorPrefix, diag.DefaultSinkIDPrefix, d.ID, "Mu.yaml", d.Message),
|
|
sink.ErrorMsgs()[0])
|
|
|
|
// Now check that this same project compiles fine if we manually specify an architecture.
|
|
sink = buildFile(Options{
|
|
Arch: backends.Arch{
|
|
Cloud: clouds.AWS,
|
|
},
|
|
}, mufile, ".yaml")
|
|
assert.Equal(t, 0, sink.Errors(), "expected no compilation errors")
|
|
}
|
|
|
|
func TestUnrecognizedCloud(t *testing.T) {
|
|
mufile := []byte("name: notarget\n" +
|
|
"abstract: true\n" +
|
|
"clusters:\n" +
|
|
" prod:\n" +
|
|
" default: true\n" +
|
|
" cloud: badcloud\n")
|
|
// Check that the compiler issued an error due to an unrecognized cloud.
|
|
sink := buildFile(Options{}, mufile, ".yaml")
|
|
d := errors.ErrorUnrecognizedCloudArch
|
|
assert.Equal(t, 1, sink.Errors(), "expected a single error")
|
|
assert.Equal(t,
|
|
fmt.Sprintf("%v: %v%v: %v: %v\n",
|
|
diag.DefaultSinkErrorPrefix, diag.DefaultSinkIDPrefix, d.ID, "Mu.yaml",
|
|
fmt.Sprintf(d.Message, "badcloud")),
|
|
sink.ErrorMsgs()[0])
|
|
}
|