diff --git a/config.go b/config.go index 1a909f4..6871270 100644 --- a/config.go +++ b/config.go @@ -20,7 +20,7 @@ type Config struct { // The path to the templates directory. Default `templates`. TemplatesDir string `json:"templates_dir,omitempty" arg:"-t,--template-dir" default:"templates"` - // The FS to load templates from. Overrides Path if not nil. + // The FS to load templates from. Overrides TemplatesDir if not nil. TemplatesFS fs.FS `json:"-" arg:"-"` // File extension to search for to find template files. Default `.html`. diff --git a/make_tool.cue b/make_tool.cue index 33981b1..e4fe9f8 100644 --- a/make_tool.cue +++ b/make_tool.cue @@ -7,9 +7,6 @@ import ( "tool/exec" "tool/file" "tool/os" - - // "encoding/json" - // "tool/cli" ) #vars: { @@ -69,32 +66,26 @@ task: build: { task: run: { vars: #vars - gobuild: task.build & {"vars": vars, outfile: "\(vars.testdir)/xtemplate"} rmdataw: file.RemoveAll & {path: "\(vars.testdir)/dataw"} mkdataw: file.Mkdir & {path: "\(vars.testdir)/dataw", $after: rmdataw.$done} - mklog: file.Create & {filename: "\(vars.testdir)/xtemplate.log", contents: ""} start: exec.Run & { - $after: mkdataw.$done && mklog.$done && gobuild.gobuild.$done - cmd: ["bash", "-c", "./xtemplate --loglevel -4 -d DB:sql:sqlite3:file:./dataw/test.sqlite -d FS:fs:./data --config-file config.json >\(mklog.filename) 2>&1"] - dir: vars.testdir - } - - ready: exec.Run & { - $after: mklog.$done - cmd: ["bash", "-c", "grep -q 'starting server' <(tail -f \(mklog.filename))"] + cmd: "./xtemplate --loglevel -4 -d DB:sql:sqlite3:file:./dataw/test.sqlite -d FS:fs:./data --config-file config.json" + dir: vars.testdir + $after: mkdataw.$done } } - task: test: { vars: #vars port: int | *8080 list: file.Glob & {glob: "\(vars.testdir)/tests/*.hurl"} + ready: exec.Run & {cmd: "curl -X GET --retry 5 --retry-connrefused --retry-delay 1 http://localhost:\(port)"} hurl: exec.Run & { cmd: ["hurl", "--continue-on-error", "--test", "--report-html", "report", "--connect-to", "localhost:8080:localhost:\(port)"] + list.files - dir: vars.testdir + dir: vars.testdir + after: ready.$done } } @@ -107,6 +98,15 @@ task: gotest: { } } +task: build_test: { + vars: #vars + + build: task.build & {"vars": vars, outfile: "\(vars.testdir)/xtemplate"} + run: task.run & {"vars": vars, start: $after: build.gobuild.$done} + test: task.test & {"vars": vars, ready: $after: run.start} + kill: exec.Run & {cmd: "pkill xtemplate", $after: test.hurl} +} + task: dist: { vars: #vars @@ -133,6 +133,20 @@ task: dist: { } } +task: build_docker: { + vars: #vars + + tags: [ + "infogulch/xtemplate:\(vars.version)", + if vars.version == vars.latest {"infogulch/xtemplate:latest"}, + ] + + build: exec.Run & { + cmd: ["docker", "build"] + list.FlattenN([for t in tags {["-t", t]}], 1) + ["--build-arg", "LDFLAGS=\(vars.ldflags)", "."] + dir: vars.rootdir + } +} + task: test_docker: { vars: #vars @@ -141,43 +155,59 @@ task: test_docker: { dir: vars.rootdir } run: exec.Run & { - cmd: "docker run -d --rm --name xtemplate-test -p 8081:80 xtemplate-test" + cmd: ["docker", "run", "-d", "--rm", "--name", "xtemplate-test", "-p", "8081:80", "xtemplate-test"] $after: build.$done } - ready: exec.Run & { - cmd: ["bash", "-c", "grep -q 'starting server' <(docker logs -f xtemplate-test)"] - $after: run.$done - } - test: task.test & {"vars": vars, port: 8081, hurl: $after: ready.$done} + test: task.test & {"vars": vars, port: 8081} stop: exec.Run & {cmd: "docker stop xtemplate-test", $after: test.hurl.$done} // be nice if we can always run this even if previous steps fail } -task: build_docker: { +task: build_caddy: { vars: #vars - tags: [ - "infogulch/xtemplate:\(vars.version)", - if vars.version == vars.latest {"infogulch/xtemplate:latest"}, - ] + osenv: os.Environ build: exec.Run & { - cmd: ["docker", "build"] + list.FlattenN([for t in tags {["-t", t]}], 1) + ["--build-arg", "LDFLAGS=\(vars.ldflags)", "."] + cmd: ["xcaddy", "build", + "--with", "github.com/infogulch/xtemplate-caddy", + "--with", "github.com/infogulch/xtemplate=.", + "--with", "github.com/infogulch/xtemplate/providers=./providers", + "--with", "github.com/infogulch/xtemplate/providers/nats=./providers/nats", + "--with", "github.com/mattn/go-sqlite3", + "--output", "./caddy"] dir: vars.rootdir + env: osenv & {CGO_ENABLED: "1"} } } -command: { - for k, t in task { - (k): {cfg: meta, vars: cfg.vars, t} +task: run_caddy: { + vars: #vars + + rmdataw: file.RemoveAll & {path: "\(vars.testdir)/dataw"} + mkdataw: file.Mkdir & {path: "\(vars.testdir)/dataw", $after: rmdataw.$done} + + start: exec.Run & { + cmd: ["../caddy", "start", "--config", "caddy.json"] + dir: vars.testdir + $after: mkdataw.$done } } -command: run_test: { - cfg: meta +task: build_test_caddy: { + vars: #vars + + build: task.build_caddy & {"vars": vars} + run_caddy: task.test_caddy & {"vars": vars, start: $after: build.build.$done} + test_caddy: task.test & {"vars": vars, port: 8082, ready: $after: run_caddy.start.$done} + kill_caddy: exec.Run & {cmd: "pkill caddy", $after: test_caddy.hurl.$done} // is there a better way? + + $done: kill_caddy.$done +} - run: task.run & {"vars": cfg.vars, start: mustSucceed: false} - test: task.test & {"vars": cfg.vars, hurl: $after: run.ready.$done} - kill: exec.Run & {cmd: "pkill xtemplate", $after: test.hurl.$done} +command: { + for k, t in task { + (k): {cfg: meta, vars: cfg.vars, t} + } } command: ci: { @@ -185,11 +215,10 @@ command: ci: { gotest: task.gotest & {"vars": cfg.vars} - run: task.run & {"vars": cfg.vars, start: mustSucceed: false} // it's killed so it will never succeed - test: task.test & {"vars": cfg.vars, hurl: $after: run.ready.$done} - kill: exec.Run & {cmd: "pkill xtemplate", $after: test.hurl.$done} // is there a better way? + build_test: task.build_test & {"vars": cfg.vars} + build_test_caddy: task.build_test_caddy & {"vars": cfg.vars, run_caddy: start: $after: build_test.kill} - dist: task.dist & {"vars": cfg.vars, rmdist: $after: kill.$done} + dist: task.dist & {"vars": cfg.vars, rmdist: $after: build_test.$done} test_docker: task.test_docker & {"vars": cfg.vars} build_docker: task.build_docker & {"vars": cfg.vars, build: $after: test_docker.stop.$done} @@ -197,4 +226,5 @@ command: ci: { cmd: ["docker", "push"] + build_docker.tags $after: build_docker.build.$done } + } diff --git a/test/caddy.json b/test/caddy.json new file mode 100644 index 0000000..79363bd --- /dev/null +++ b/test/caddy.json @@ -0,0 +1,59 @@ +{ + "apps": { + "http": { + "servers": { + "": { + "listen": [ + ":8082" + ], + "routes": [ + { + "handle": [ + { + "handler": "xtemplate", + "minify": true, + "dot": [ + { + "type": "sql", + "name": "DB", + "driver": "sqlite3", + "connstr": "file:./dataw/test.sqlite" + }, + { + "type": "fs", + "name": "FS", + "path": "./data" + }, + { + "type": "fs", + "name": "FSW", + "path": "./dataw" + }, + { + "type": "fs", + "name": "Migrations", + "path": "./migrations" + }, + { + "type": "kv", + "name": "KV", + "values": { + "a": "1", + "b": "2", + "hello": "world" + } + }, + { + "type": "nats", + "name": "Nats" + } + ] + } + ] + } + ] + } + } + } + } +} \ No newline at end of file diff --git a/test/templates/ready.html b/test/templates/ready.html new file mode 100644 index 0000000..a0aba93 --- /dev/null +++ b/test/templates/ready.html @@ -0,0 +1 @@ +OK \ No newline at end of file