From 4cd53cebe1334c4ebfb0b22b4c34187ab4664d4d Mon Sep 17 00:00:00 2001 From: Xavier Petit <32063953+xpetit@users.noreply.github.com> Date: Fri, 15 May 2020 12:45:52 +0200 Subject: [PATCH] working-ish --- go/Dockerfile | 22 +++++++++++++++++----- go/entrypoint.sh | 48 ++++++++++++++++++++++++++--------------------- go/src/lib/lib.go | 4 ++-- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/go/Dockerfile b/go/Dockerfile index fbf096854..3bfd41586 100644 --- a/go/Dockerfile +++ b/go/Dockerfile @@ -1,8 +1,20 @@ FROM golang:1-alpine -RUN apk add git +RUN apk add --no-cache git nano RUN go get golang.org/x/tools/cmd/goimports -RUN go get github.com/01-edu/z01 -RUN go get github.com/01-edu/public/rc -COPY . /app -ENTRYPOINT ["/bin/sh", "/app/entrypoint.sh"] +WORKDIR src +COPY entrypoint.sh /usr/local/bin +COPY src . +RUN go install rc +RUN find -mindepth 2 -maxdepth 2 -name '*_correct' -type d|xargs go install +RUN find -mindepth 2 -maxdepth 2 -name '*_correct' -type d|cut -d/ -f1-2|xargs go install +RUN find -mindepth 2 -maxdepth 2 -name '*_correct' -type d|cut -d/ -f1-2|xargs rm -rf +WORKDIR /tmp +USER 1000 +ENTRYPOINT ["/bin/sh", "/usr/local/bin/entrypoint.sh"] +# compare performance with and without : +# ENV CGO_ENABLED 0 +# -trimpath -ldflags="-s -w" +# -i +# -a (to rebuild everything and prepare .a files) +# git clone in Dockerfile to make it work anywhere (no COPY) diff --git a/go/entrypoint.sh b/go/entrypoint.sh index 69e2ba2e9..bc591de4a 100644 --- a/go/entrypoint.sh +++ b/go/entrypoint.sh @@ -5,30 +5,36 @@ set -o pipefail IFS=' ' -cp -rT /app /jail +mkdir -p src/student +cd src/student -if test "$EXPECTED_FILES"; then - echo -n 'Formatting (with `goimports -d .`) ... ' - cd /jail/student - s=$(goimports -d .) - if test "$s"; then - echo - echo "$s" - exit 1 - fi - echo OK - if test "$ALLOWED_FUNCTIONS"; then - rc "$EXPECTED_FILES" $ALLOWED_FUNCTIONS - fi +if test "$REPOSITORY"; then + git clone --depth=1 --shallow-submodules "$REPOSITORY" . fi -cd /jail - -if ! test -f ${EXERCISE}_test.go; then - echo No test file found for the exercise : "$EXERCISE" +# Check formatting +s=$(goimports -d .) +if test "$s"; then + echo '$ goimports -d .' + echo "$s" exit 1 fi -# TODO: maybe change btree exercises so that they work without student code and then delete this line -find . -name '*_test.go' ! -name ${EXERCISE}_test.go -delete -go test -failfast '-run=(?i)'test${EXERCISE} +# Check restrictions +if test "$ALLOWED_FUNCTIONS"; then + for file in $EXPECTED_FILES; do + rc "$file" $ALLOWED_FUNCTIONS + done +fi + +# Compile test +cd +GOPATH=$GOPATH:$HOME +if command -v "$EXERCISE"_test &>/dev/null; then + # The exercise is a program + go build "student/$EXERCISE" + "$EXERCISE"_test +else + # The exercise is a function + go run "$EXERCISE"_test +fi diff --git a/go/src/lib/lib.go b/go/src/lib/lib.go index 2be6bf85d..2093bb7e6 100644 --- a/go/src/lib/lib.go +++ b/go/src/lib/lib.go @@ -362,8 +362,8 @@ func ChallengeMain(exercise string, args ...string) { code := func(code int) string { return fmt.Sprintf("echo $?\n%d\n$", code) } - student, studentCode := run("./student") - solution, solutionCode := run("test_" + exercise) + student, studentCode := run("./" + exercise) + solution, solutionCode := run(exercise + "_correct") if solutionCode != 0 { if studentCode == 0 { Fatalln("Your program does not fail when it should (with a non-zero exit status) :" + "\n" +