lang, engine: Add a facility for resources to export constants

Since we focus on safety, it would be nice to reduce the chance of any
runtime errors if we made a typo for a resource parameter. With this
patch, each resource can export constants into the global namespace so
that typos would cause a compile error.

Of course in the future if we had a more advanced type system, then we
could support precise types for each individual resource param, but in
an attempt to keep things simple, we'll leave that for another day. It
would add complexity too if we ever wanted to store a parameter
externally.

Lastly, we might consider adding "special case" parsing so that directly
specified fields would parse intelligently. For example, we could allow:

	file "/tmp/hello" {
		state => exists,	# magic sugar!
	}

This isn't supported for now, but if it works after all the other parser
changes have been made, it might be something to consider.
This commit is contained in:
James Shubin
2020-01-29 10:49:48 -05:00
parent f67ad9c061
commit 90fd8023dd
61 changed files with 536 additions and 138 deletions

View File

@@ -228,7 +228,7 @@ func TestResources1(t *testing.T) {
p := "/tmp/whatever"
s := "hello, world\n"
res.Path = p
res.State = "exists"
res.State = FileStateExists
contents := s
res.Content = &contents
@@ -292,7 +292,7 @@ func TestResources1(t *testing.T) {
res := r.(*FileRes) // if this panics, the test will panic
p := "/tmp/emptyfile"
res.Path = p
res.State = "exists"
res.State = FileStateExists
timeline := []Step{
NewStartupStep(1000 * 60), // startup
@@ -316,7 +316,7 @@ func TestResources1(t *testing.T) {
res := r.(*FileRes) // if this panics, the test will panic
p := "/tmp/existingfile"
res.Path = p
res.State = "exists"
res.State = FileStateExists
content := "some existing text\n"
timeline := []Step{
@@ -811,14 +811,14 @@ func TestResources2(t *testing.T) {
testCases := []test{}
{
//file "/tmp/somefile" {
// state => "exists",
// state => $const.res.file.state.exists,
// content => "some new text\n",
//}
r1 := makeRes("file", "r1")
res := r1.(*FileRes) // if this panics, the test will panic
p := "/tmp/somefile"
res.Path = p
res.State = "exists"
res.State = FileStateExists
content := "some new text\n"
res.Content = &content
@@ -850,7 +850,7 @@ func TestResources2(t *testing.T) {
res := r1.(*FileRes) // if this panics, the test will panic
p := "/tmp/somefile"
res.Path = p
//res.State = "exists" // not specified!
//res.State = FileStateExists // not specified!
content := "some new text\n"
res.Content = &content
@@ -883,7 +883,7 @@ func TestResources2(t *testing.T) {
res := r1.(*FileRes) // if this panics, the test will panic
p := "/tmp/somefile"
res.Path = p
//res.State = "exists" // not specified!
//res.State = FileStateExists // not specified!
content := "some new text\n"
res.Content = &content
@@ -907,14 +907,14 @@ func TestResources2(t *testing.T) {
}
{
//file "/tmp/somefile" {
// state => "absent",
// state => $const.res.file.state.absent,
//}
// and no existing file exists!
r1 := makeRes("file", "r1")
res := r1.(*FileRes) // if this panics, the test will panic
p := "/tmp/somefile"
res.Path = p
res.State = "absent"
res.State = FileStateAbsent
timeline := []func() error{
fileRemove(p), // nothing here
@@ -936,14 +936,14 @@ func TestResources2(t *testing.T) {
}
{
//file "/tmp/somefile" {
// state => "absent",
// state => $const.res.file.state.absent,
//}
// and a file already exists!
r1 := makeRes("file", "r1")
res := r1.(*FileRes) // if this panics, the test will panic
p := "/tmp/somefile"
res.Path = p
res.State = "absent"
res.State = FileStateAbsent
timeline := []func() error{
fileWrite(p, "whatever"),
@@ -966,7 +966,7 @@ func TestResources2(t *testing.T) {
{
//file "/tmp/somefile" {
// content => "some new text\n",
// state => "exists",
// state => $const.res.file.state.exists,
//
// Meta:reverse => true,
//}
@@ -974,7 +974,7 @@ func TestResources2(t *testing.T) {
res := r1.(*FileRes) // if this panics, the test will panic
p := "/tmp/somefile"
res.Path = p
res.State = "exists"
res.State = FileStateExists
content := "some new text\n"
res.Content = &content
original := "this is the original state\n" // original state
@@ -1035,7 +1035,7 @@ func TestResources2(t *testing.T) {
res := r1.(*FileRes) // if this panics, the test will panic
p := "/tmp/somefile"
res.Path = p
//res.State = "exists" // unspecified
//res.State = FileStateExists // unspecified
content := "some new text\n"
res.Content = &content
original := "this is the original state\n" // original state
@@ -1100,7 +1100,7 @@ func TestResources2(t *testing.T) {
res := r1.(*FileRes) // if this panics, the test will panic
p := "/tmp/somefile"
res.Path = p
//res.State = "exists" // unspecified
//res.State = FileStateExists // unspecified
content := "some new text\n"
res.Content = &content
var r2 engine.Res // future reversed resource
@@ -1149,7 +1149,7 @@ func TestResources2(t *testing.T) {
}
{
//file "/tmp/somefile" {
// state => "absent",
// state => $const.res.file.state.absent,
//
// Meta:reverse => true,
//}
@@ -1157,7 +1157,7 @@ func TestResources2(t *testing.T) {
res := r1.(*FileRes) // if this panics, the test will panic
p := "/tmp/somefile"
res.Path = p
res.State = "absent"
res.State = FileStateAbsent
original := "this is the original state\n" // original state
var r2 engine.Res // future reversed resource
@@ -1207,7 +1207,7 @@ func TestResources2(t *testing.T) {
}
{
//file "/tmp/somefile" {
// state => "exists",
// state => $const.res.file.state.exists,
// fragments => [
// "/tmp/frag1",
// "/tmp/fragdir1/",
@@ -1220,7 +1220,7 @@ func TestResources2(t *testing.T) {
res := r1.(*FileRes) // if this panics, the test will panic
p := "/tmp/somefile"
res.Path = p
res.State = "exists"
res.State = FileStateExists
res.Fragments = []string{
"/tmp/frag1",
"/tmp/fragdir1/",
@@ -1272,7 +1272,7 @@ func TestResources2(t *testing.T) {
}
{
//file "/tmp/somefile" {
// state => "exists",
// state => $const.res.file.state.exists,
// source => "/tmp/somefiletocopy",
//}
r1 := makeRes("file", "r1")
@@ -1281,7 +1281,7 @@ func TestResources2(t *testing.T) {
p2 := "/tmp/somefiletocopy"
content := "hello this is some file to copy\n"
res.Path = p
res.State = "exists"
res.State = FileStateExists
res.Source = p2
timeline := []func() error{
@@ -1308,13 +1308,13 @@ func TestResources2(t *testing.T) {
}
{
//file "/tmp/somedir/" {
// state => "exists",
// state => $const.res.file.state.exists,
//}
r1 := makeRes("file", "r1")
res := r1.(*FileRes) // if this panics, the test will panic
p := "/tmp/somedir/"
res.Path = p
res.State = "exists"
res.State = FileStateExists
timeline := []func() error{
fileAbsent(p), // ensure it's absent
@@ -1337,7 +1337,7 @@ func TestResources2(t *testing.T) {
}
{
//file "/tmp/somedir/" {
// state => "exists",
// state => $const.res.file.state.exists,
// source => /tmp/somedirtocopy/,
// recurse => true,
//}
@@ -1346,7 +1346,7 @@ func TestResources2(t *testing.T) {
p := "/tmp/somedir/"
p2 := "/tmp/somedirtocopy/"
res.Path = p
res.State = "exists"
res.State = FileStateExists
res.Source = p2
res.Recurse = true
@@ -1408,7 +1408,7 @@ func TestResources2(t *testing.T) {
}
{
//file "/tmp/somedir/" {
// state => "exists",
// state => $const.res.file.state.exists,
// recurse => true,
// purge => true,
//}
@@ -1416,7 +1416,7 @@ func TestResources2(t *testing.T) {
res := r1.(*FileRes) // if this panics, the test will panic
p := "/tmp/somedir/"
res.Path = p
res.State = "exists"
res.State = FileStateExists
res.Recurse = true
res.Purge = true
@@ -1474,7 +1474,7 @@ func TestResources2(t *testing.T) {
}
{
//file "/tmp/somedir/" {
// state => "exists",
// state => $const.res.file.state.exists,
// recurse => true,
// purge => true,
//}
@@ -1489,7 +1489,7 @@ func TestResources2(t *testing.T) {
res := r1.(*FileRes) // if this panics, the test will panic
p := "/tmp/somedir/"
res.Path = p
res.State = "exists"
res.State = FileStateExists
res.Recurse = true
res.Purge = true