Skip to content

Commit

Permalink
Add files
Browse files Browse the repository at this point in the history
  • Loading branch information
kazk committed Sep 8, 2022
1 parent e5ee316 commit 9013534
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.git/
bin/
examples/
README.md
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: CI
on:
push:
branches:
- main
pull_request:

jobs:
build:
runs-on: ubuntu-latest
if: ${{ github.repository == 'codewars/purescript' }}
steps:
- uses: actions/checkout@v2
- uses: docker/setup-buildx-action@v2

- name: Build image
uses: docker/build-push-action@v3
with:
context: .
push: false
# Make the image available in next step
load: true
tags: ghcr.io/codewars/purescript:latest
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Run Passing Example
run: bin/run passing

- name: Report Image Size
run: |
echo "## Image Size" >> $GITHUB_STEP_SUMMARY
docker image inspect --format '{{.Size}}' ghcr.io/codewars/purescript:latest | numfmt --to=si --suffix=B >> $GITHUB_STEP_SUMMARY
39 changes: 39 additions & 0 deletions .github/workflows/push-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Build and push a Docker image to GitHub Container Registry when
# a new tag is pushed.
name: Push Image

on:
push:
tags:
- "*"

jobs:
build-and-push-image:
if: ${{ github.repository == 'codewars/purescript' }}
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: codewars
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push image
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: |
ghcr.io/codewars/purescript:latest
ghcr.io/codewars/purescript:${{ github.ref_name }}
cache-from: type=gha
cache-to: type=gha,mode=max
41 changes: 41 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
FROM buildpack-deps:bionic

RUN set -ex; \
useradd --create-home codewarrior; \
ln -s /home/codewarrior /workspace;

RUN set -ex; \
curl -sL https://deb.nodesource.com/setup_8.x | bash -; \
apt-get install -y nodejs; \
apt-get clean; \
rm -rf /var/lib/apt/lists/* /tmp/*;

RUN set -ex; \
mkdir -p /opt/purescript; \
curl -fsSL https://github.com/purescript/purescript/releases/download/v0.12.2/linux64.tar.gz | tar xz -C /opt/purescript --strip-components=1; \
curl -fsSL https://github.com/purescript/psc-package/releases/download/v0.5.1/linux64.tar.gz | tar xz -C /opt/purescript --strip-components=1;

RUN npm install -g pulp@12.3.1;

COPY --chown=codewarrior:codewarrior workspace/ /workspace
WORKDIR /workspace

USER codewarrior

ENV USER=codewarrior \
HOME=/home/codewarrior \
PATH=/opt/purescript:$PATH

RUN set -ex; \
cd /workspace; \
npm install; \
# install packages
psc-package install; \
# compile packages
psc-package build; \
# ensure running `Main.purs` works
pulp run; \
# ensure testing works
pulp test || true; \
# clean up
rm -rf ./src/Main.purs ./output/Main/ ./test/Example/ ./output/Example.ExampleSpec/;
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# purescript

Container image for PureScript

## Usage

```bash
W=/workspace
# Create container
C=$(docker container create --rm -w $W ghcr.io/codewars/purescript:latest pulp test)

# Copy files from the examples directory
docker container cp ./examples/passing/. $C:$W

# Start
docker container start --attach $C
```

## Building

```bash
docker build -t ghcr.io/codewars/purescript:latest .
```
17 changes: 17 additions & 0 deletions bin/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
set -eu

if [ -z "${IMAGE:+x}" ]; then
IMAGE=ghcr.io/codewars/purescript:latest
fi

W=/workspace

# Create container
C=$(docker container create --rm -w $W $IMAGE pulp test)

# Copy files from the examples directory
docker container cp examples/${1:-passing}/. $C:$W

# Run tests
docker container start --attach $C
5 changes: 5 additions & 0 deletions examples/passing/src/Example.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Example where
import Prelude

add' :: Int -> Int -> Int
add' a b = a + b
15 changes: 15 additions & 0 deletions examples/passing/test/ExampleSpec.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module ExampleSpec where

import Prelude
import Example (add')

import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)

spec :: Spec Unit
spec =
describe "Example" do
describe "add" do
it "returns sum" do
let sum = add' 1 1
sum `shouldEqual` 2
13 changes: 13 additions & 0 deletions workspace/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions workspace/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "cw-purescript",
"version": "0.0.1",
"description": "",
"dependencies": {
"big-integer": "^1.6.41"
}
}
17 changes: 17 additions & 0 deletions workspace/psc-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "cw-purescript",
"set": "psc-0.12.2-20190119",
"source": "https://github.com/purescript/package-sets.git",
"depends": [
"prelude",
"console",
"debug",
"effect",
"bigints",
"rationals",
"profunctor-lenses",
"spec",
"spec-discovery",
"spec-quickcheck"
]
}
9 changes: 9 additions & 0 deletions workspace/src/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Main where

import Prelude
import Effect (Effect)
import Effect.Console (log)

main :: Effect Unit
main = do
log "Hello World!"
49 changes: 49 additions & 0 deletions workspace/src/Test/Spec/Reporter/Codewars.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Test.Spec.Reporter.Codewars (codewarsReporter) where

import Prelude

import Data.Maybe (Maybe(..))
import Data.String
( Pattern(Pattern)
, Replacement(Replacement)
, replaceAll
)
import Effect.Console (log)

import Test.Spec.Reporter.Base (defaultReporter)
import Test.Spec.Runner (Reporter)
import Test.Spec.Runner.Event as Event


codewarsReporter :: Reporter
codewarsReporter = defaultReporter {} update
where
update s = case _ of
Event.Start _ -> pure s
Event.End _ -> pure s
Event.Suite name -> s <$ log ("\n<DESCRIBE::>" <> name)
Event.SuiteEnd -> s <$ log "\n<COMPLETEDIN::>"

Event.Pending name -> s <$ do
log $ "\n<IT::>" <> name
log $ "\n<LOG::>Pending Test"
log $ "\n<COMPLETEDIN::>"

Event.Pass name _ ms -> s <$ do
log $ "\n<IT::>" <> name
log $ "\n<PASSED::>Test Passed"
log $ "\n<COMPLETEDIN::>" <> (show ms)

Event.Fail name msg mStack -> s <$ do
log $ "\n<IT::>" <> name
log $ "\n<FAILED::>Test Failed<:LF:>" <> (escapeLF msg)
case mStack of
Nothing -> pure unit
Just k -> log $ "\n<LOG::-Stack Trace>" <> (escapeLF k)
log $ "\n<COMPLETEDIN::>"

_ -> pure s


escapeLF :: String -> String
escapeLF = replaceAll (Pattern "\n") (Replacement "<:LF:>")
36 changes: 36 additions & 0 deletions workspace/test/Example/ExampleSpec.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Example.ExampleSpec where

import Prelude
import Effect.Aff (delay)
import Data.Time.Duration (Milliseconds(..))

import Test.QuickCheck ((===), (/==))
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)
import Test.Spec.QuickCheck (quickCheck)

-- example from README plus basic sanity checks
spec :: Spec Unit
spec =
describe "purescript-spec" do
describe "Attributes" do
it "awesome" do
let isAwesome = true
isAwesome `shouldEqual` true

describe "Features" do
it "runs in NodeJS" $ pure unit
it "runs in the browser" $ pure unit
it "supports async specs" do
res <- delay (Milliseconds 100.0) *> pure "Alligator"
res `shouldEqual` "Alligator"

describe "QuickCheck" do
it "works" $
quickCheck \n -> (n * 2 / 2) === n
it "works again" $
quickCheck \n -> ((n + 1) * 2) /== n

describe "Failure example" do
it "should fail" do
1 `shouldEqual` 2
15 changes: 15 additions & 0 deletions workspace/test/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Test.Main where

import Prelude
import Data.Maybe (Maybe(..))
import Effect (Effect)

import Test.Spec.Discovery (discover)
import Test.Spec.Runner (defaultConfig, run')

import Test.Spec.Reporter.Codewars (codewarsReporter)

main :: Effect Unit
main = discover ".+Spec" >>= run' config [codewarsReporter]
where
config = defaultConfig { timeout = Just 12000 }

0 comments on commit 9013534

Please sign in to comment.