Implemented error messages

This commit is contained in:
Eduard Urbach 2024-06-13 12:13:32 +02:00
parent 423526e567
commit e3b26c79f4
Signed by: eduard
GPG key ID: 49226B848C78F6C8
16 changed files with 362 additions and 60 deletions

View file

@ -11,11 +11,12 @@ import (
// Build builds an executable.
func Build(args []string) int {
b := build.New()
writeExecutable := true
for i := 0; i < len(args); i++ {
switch args[i] {
case "--dry":
b.WriteExecutable = false
writeExecutable = false
case "--verbose", "-v":
config.Verbose = true
@ -34,7 +35,20 @@ func Build(args []string) int {
b.Files = append(b.Files, ".")
}
err := b.Run()
result, err := b.Run()
if err != nil {
fmt.Println(err)
return 1
}
if !writeExecutable {
return 0
}
path := b.Executable()
code, data := build.Finalize(result)
err = build.Write(path, code, data)
if err != nil {
fmt.Println(err)

View file

@ -6,7 +6,7 @@ import "fmt"
func Help(args []string) int {
fmt.Println("Usage: q [command] [options]")
fmt.Println("")
fmt.Println(" build [directory]")
fmt.Println(" build [directory] [file]")
fmt.Println(" system")
return 2
}

View file

@ -1,8 +1,6 @@
package cli_test
import (
"fmt"
"os"
"testing"
"git.akyoto.dev/cli/q/src/cli"
@ -19,17 +17,16 @@ func TestCLI(t *testing.T) {
{[]string{}, 2},
{[]string{"invalid"}, 2},
{[]string{"system"}, 0},
{[]string{"build", "non-existing-directory"}, 1},
{[]string{"build", "../../examples/hello/hello.q"}, 1},
{[]string{"build", "invalid-directory"}, 1},
{[]string{"build", "--invalid-parameter"}, 2},
{[]string{"build", "../../examples/hello", "--invalid"}, 2},
{[]string{"build", "../../examples/hello", "--dry"}, 0},
{[]string{"build", "../../examples/hello", "--dry", "--verbose"}, 0},
{[]string{"build", "../../examples/hello/hello.q", "--dry"}, 0},
}
for _, test := range tests {
t.Log(test.arguments)
directory, _ := os.Getwd()
fmt.Println(directory)
exitCode := cli.Main(test.arguments)
assert.Equal(t, exitCode, test.expectedExitCode)
}