Skip to content

Commit

Permalink
Merge pull request #47 from yassinebenaid/dev
Browse files Browse the repository at this point in the history
Added generator tests && Dockerize the app
  • Loading branch information
yassinebenaid authored Dec 11, 2024
2 parents a1e495a + ede1de5 commit 807f2d2
Show file tree
Hide file tree
Showing 5 changed files with 560 additions and 4 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/dockerize.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
name: Create and publish a Docker image

on:
release:
types: [published]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
id: push
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang:1.22

WORKDIR /bunster

COPY . .

RUN go mod download

RUN CGO_ENABLED=0 go build -o /usr/local/bin/bunster ./cmd/bunster

RUN rm -rf /bunster

CMD ["bash"]
14 changes: 10 additions & 4 deletions generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ import (
"github.com/yassinebenaid/bunster/lexer"
"github.com/yassinebenaid/bunster/parser"
"github.com/yassinebenaid/bunster/pkg/dottest"
"github.com/yassinebenaid/godump"
)

var dump = (&godump.Dumper{
Theme: godump.DefaultTheme,
ShowPrimitiveNamedTypes: true,
}).Sprintln

func TestGenerator(t *testing.T) {
testFiles, err := filepath.Glob("./tests/*.test")
if err != nil {
Expand All @@ -36,23 +42,23 @@ func TestGenerator(t *testing.T) {
for i, test := range tests {
script, err := parser.Parse(lexer.New([]byte(test.Input)))
if err != nil {
t.Fatalf("#%d: parser error.\nError: %s", i, err)
t.Fatalf("\nTest: %sError: %s", dump(test.Label), dump(err.Error()))
}

program := generator.Generate(script)
formattedProgram, gofmtErr, err := gofmt(program.String())
if err != nil {
t.Fatalf("#%d: error when trying to format the generated program.\nError: %s.\nStderr: %s", i, err, gofmtErr)
t.Fatalf("\n#%d: error when trying to format the generated program.\nError: %s.\nStderr: %s", i, err, gofmtErr)
}

formattedTestOutput, gofmtErr, err := gofmt(test.Output)
if err != nil {
t.Fatalf("#%d: error when trying to format the test expected output.\nError: %s.\nStderr: %s", i, err, gofmtErr)
t.Fatalf("\n#%d: error when trying to format the test expected output.\nError: %s.\nStderr: %s", i, err, gofmtErr)
}

if formattedProgram != formattedTestOutput {
t.Fatalf(
"#%d: The generated program doesn't match the expected output.\n Program:\n%s",
"\n#%d: The generated program doesn't match the expected output.\n Program:\n%s",
i, diffStrings(formattedTestOutput, formattedProgram),
)
}
Expand Down
42 changes: 42 additions & 0 deletions generator/tests/0-simple-commands.test
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,45 @@ func Main(shell *runtime.Shell) {
}()
}
#(ENDTEST)


#(TEST: multiline command with arguments)

cmd --option=value \
--option='value' \
--option="value" \
-o

#(RESULT)

package main

import "bunster-build/runtime"

func Main(shell *runtime.Shell) {
func() {
var cmd_0_name = `cmd`
var cmd_0_args []string
cmd_0_args = append(cmd_0_args, `--option=value`)
cmd_0_args = append(cmd_0_args, `--option=value`)
cmd_0_args = append(cmd_0_args, `--option=value`)
cmd_0_args = append(cmd_0_args, `-o`)
var cmd_0 = shell.Command(cmd_0_name, cmd_0_args...)
cmd_0_fdt, err := shell.CloneFDT()
if err != nil {
shell.HandleError("", err)
return
}
defer cmd_0_fdt.Destroy()
cmd_0.Stdin = cmd_0_fdt.Get(`0`)
cmd_0.Stdout = cmd_0_fdt.Get(`1`)
cmd_0.Stderr = cmd_0_fdt.Get(`2`)
if err := cmd_0.Run(); err != nil {
shell.HandleError(cmd_0_name, err)
return
}
shell.ExitCode = cmd_0.ProcessState.ExitCode()

}()
}
#(ENDTEST)
Loading

0 comments on commit 807f2d2

Please sign in to comment.