2018-11-07 21:13:47 +00:00
|
|
|
package buildfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-11-18 21:49:11 +00:00
|
|
|
"git.xdrm.io/go/nix-amer/internal/instruction"
|
2018-11-07 21:13:47 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-11-11 20:38:16 +00:00
|
|
|
func TestNullContext(t *testing.T) {
|
|
|
|
buffer := bytes.NewBufferString("")
|
|
|
|
_, err := NewReader(nil, buffer)
|
|
|
|
if err != ErrNullContext {
|
|
|
|
t.Fatalf("expected <%s>, got <%v>", ErrNullContext, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestIgnoreCommentsAndEmptyLines(t *testing.T) {
|
|
|
|
ctx, _ := instruction.CreateContext("apt-get", "")
|
2018-11-14 10:38:21 +00:00
|
|
|
buffer := bytes.NewBufferString("# some comment\n;other comment\n \t \n\t \t\n; other comment after empty lines")
|
2018-11-11 20:38:16 +00:00
|
|
|
|
|
|
|
r, err := NewReader(ctx, buffer)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error <%v>", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Content) > 0 {
|
|
|
|
t.Fatalf("expected no content, got %d instructions", len(r.Content))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:13:47 +00:00
|
|
|
func TestInstructionSyntax(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
File string
|
|
|
|
Line int
|
|
|
|
Err error
|
|
|
|
}{
|
2018-11-14 10:38:21 +00:00
|
|
|
{"[pre]\ninstall args\ndelete args\n", 1, nil},
|
|
|
|
{"[pre]\n install args\ndelete args\n", 1, nil},
|
|
|
|
{"[pre]\n\tinstall args\ndelete args\n", 1, nil},
|
|
|
|
{"[pre]\n \t install args\ndelete args\n", 1, nil},
|
|
|
|
{"[pre]\n \t install args\ndelete args\n", 1, nil},
|
|
|
|
|
|
|
|
{"[pre]\ncmd args\ncmd args\n", 2, instruction.ErrUnknownInstruction},
|
|
|
|
{"[pre]\ninstall args\ncmd args\n", 3, instruction.ErrUnknownInstruction},
|
|
|
|
{"[pre]\n cmd args\ncmd args\n", 2, instruction.ErrUnknownInstruction},
|
|
|
|
{"[pre]\n\tcmd args\ncmd args\n", 2, instruction.ErrUnknownInstruction},
|
|
|
|
{"[pre]\n \t cmd args\ncmd args\n", 2, instruction.ErrUnknownInstruction},
|
|
|
|
{"[pre]\n \t cmd args\ncmd args\n", 2, instruction.ErrUnknownInstruction},
|
|
|
|
|
|
|
|
{"[pre]\ncmd args\ncmd\n", 2, instruction.ErrUnknownInstruction},
|
|
|
|
{"[pre]\ninstall\ncmd args\n", 2, instruction.ErrInvalidSyntax},
|
|
|
|
|
|
|
|
{"[pre]\ninstall args\n cmd args\n", 3, instruction.ErrUnknownInstruction},
|
|
|
|
{"[pre]\ninstall args\ncmd\n", 3, instruction.ErrInvalidSyntax},
|
2018-11-07 21:13:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 11:37:31 +00:00
|
|
|
ctx, _ := instruction.CreateContext("apt-get", "")
|
|
|
|
|
2018-11-07 21:13:47 +00:00
|
|
|
for i, test := range tests {
|
|
|
|
|
|
|
|
// create reader
|
|
|
|
buffer := bytes.NewBufferString(test.File)
|
2018-11-10 11:37:31 +00:00
|
|
|
_, err := NewReader(ctx, buffer)
|
2018-11-07 21:13:47 +00:00
|
|
|
|
|
|
|
// no error expected
|
|
|
|
if test.Err == nil {
|
|
|
|
if err != nil {
|
2018-11-14 10:38:21 +00:00
|
|
|
lineerr, ok := err.(LineError)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("[%d] expect error to be of type <LineError>", i)
|
|
|
|
continue
|
|
|
|
}
|
2018-11-07 21:13:47 +00:00
|
|
|
t.Errorf("[%d] expect no error, got <%s>", i, lineerr.Err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if err == nil {
|
|
|
|
t.Errorf("[%d] expect error <%s>, got none", i, test.Err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
lineerr := err.(LineError)
|
|
|
|
|
|
|
|
// error expected
|
|
|
|
if lineerr.Err != test.Err {
|
|
|
|
t.Errorf("[%d] expect error <%s>, got <%s>", i, test.Err, lineerr.Err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if lineerr.Line != test.Line {
|
|
|
|
t.Errorf("[%d] expect error at line %d, got %d", i, test.Line, lineerr.Line)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|