From c3d319f2b3076a0bb169bcd8a7b6a011f6aba9a5 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 7 Jun 2018 19:28:31 +0200 Subject: [PATCH] Initial commit - based on standard project template --- .editorconfig | 22 +++++++ .gitignore | 16 +++++ Cargo.toml | 17 ++++++ Makefile | 43 +++++++++++++ README.md | 3 + etc/developer.Dockerfile | 8 +++ src/main.rs | 4 ++ tests/stateless-journey.sh | 14 +++++ tests/utilities.sh | 122 +++++++++++++++++++++++++++++++++++++ 9 files changed, 249 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 Makefile create mode 100644 README.md create mode 100644 etc/developer.Dockerfile create mode 100644 src/main.rs create mode 100755 tests/stateless-journey.sh create mode 100644 tests/utilities.sh diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000000..762b67e9883 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,22 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true + +[*.sh] +indent_style = space +indent_size = 2 + +# Tab indentation (no size specified) +[Makefile] +indent_style = tab + +# Matches the exact files either package.json or .travis.yml +[{package.json,.travis.yml}] +indent_style = space +indent_size = 2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..25adb761d1a --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# Created by .ignore support plugin (hsz.mobi) +### Rust template +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +/.idea/ + +/callgrind.profile diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000000..70bd302018c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "grit-cli" +version = "1.0.0" +authors = ["Sebastian Thiel "] +publish = false + +[dependencies] +failure = "0.1.1" +failure-tools = "4.0.2" + +[[bin]] +name="grit" +path="src/main.rs" + +[profile.release] +panic = 'unwind' +incremental = false diff --git a/Makefile b/Makefile new file mode 100644 index 00000000000..60cabb00045 --- /dev/null +++ b/Makefile @@ -0,0 +1,43 @@ +fixture = tests/fixtures/input.txt +bench_fixture = tests/fixtures/big-input.txt +docker_image = gitrs_docker_developer_environment + +help: + $(info -Targets -----------------------------------------------------------------------------) + $(info -- Use docker for all dependencies - run make interactively from there ----------------) + $(info interactive-developer-environment-in-docker | gives you everything you need to run all targets) + $(info -Development Targets -----------------------------------------------------------------) + $(info lint | run lints with clippy) + $(info benchmark | just for fun, really) + $(info profile | only on linux - run callgrind and annotate it) + $(info journey-tests | run all stateless journey test) + $(info continuous-journey-tests | run all stateless journey test whenever something changes) + +always: + +interactive-developer-environment-in-docker: + docker build -t $(docker_image) - < etc/developer.Dockerfile + docker run -v $$PWD:/volume -w /volume -it $(docker_image) + +target/debug/rit: always + cargo build + +target/release/rit: always + cargo build --release + +lint: + cargo clippy + +profile: target/release/rit + valgrind --callgrind-out-file=callgrind.profile --tool=callgrind $< $(bench_fixture) >/dev/null + callgrind_annotate --auto=yes callgrind.profile + +benchmark: target/release/rit + hyperfine '$< $(bench_fixture)' + +journey-tests: target/debug/rit + ./tests/stateless-journey.sh $< + +continuous-journey-tests: + watchexec $(MAKE) journey-tests + diff --git a/README.md b/README.md new file mode 100644 index 00000000000..4c6b14e701c --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +## Goal + +TBD diff --git a/etc/developer.Dockerfile b/etc/developer.Dockerfile new file mode 100644 index 00000000000..42b52bc24e1 --- /dev/null +++ b/etc/developer.Dockerfile @@ -0,0 +1,8 @@ +from guangie88/rustfmt-clippy:nightly + +run cargo install hyperfine watchexec + +run apt-get update +run apt-get install -y valgrind + +env PATH=$PATH:/root/.cargo/bin diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 00000000000..aa7565cf3bd --- /dev/null +++ b/src/main.rs @@ -0,0 +1,4 @@ +extern crate failure; +extern crate failure_tools; + +fn main() {} diff --git a/tests/stateless-journey.sh b/tests/stateless-journey.sh new file mode 100755 index 00000000000..194436d681e --- /dev/null +++ b/tests/stateless-journey.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -eu + +exe=${1:?First argument must be the executable to test} + +root="$(cd "${0%/*}" && pwd)" +# shellcheck disable=1090 +source "$root/utilities.sh" +snapshot="$root/snapshots" +fixture="$root/fixtures" + +SUCCESSFULLY=0 +WITH_FAILURE=1 + diff --git a/tests/utilities.sh b/tests/utilities.sh new file mode 100644 index 00000000000..ad8d6f33901 --- /dev/null +++ b/tests/utilities.sh @@ -0,0 +1,122 @@ +#!/bin/bash + +WHITE="$(tput setaf 9 2>/dev/null || echo -n '')" +YELLOW="$(tput setaf 3 2>/dev/null || echo -n '')" +GREEN="$(tput setaf 2 2>/dev/null || echo -n '')" +RED="$(tput setaf 1 2>/dev/null || echo -n '')" +OFFSET=( ) +STEP=" " + +function title () { + echo "$WHITE-----------------------------------------------------" + echo "${GREEN}$*" + echo "$WHITE-----------------------------------------------------" +} + +function _context () { + local name="${1:?}" + shift + echo 1>&2 "${YELLOW}${OFFSET[*]:-}[$name] $*" + OFFSET+=("$STEP") +} + +function with () { + _context with "$*" +} + +function when () { + _context when "$*" +} + +function _note () { + local name="${1:?}" + local color="${2:-}" + shift 2 + echo 1>&2 -n "${OFFSET[*]:-}${color}[$name] ${*// /}" +} + +function it () { + _note it "${GREEN}" "$*" +} + +function precondition () { + _note precondition "${WHITE}" "$*" +} + +function shortcoming () { + _note shortcoming "${RED}" "$*" +} + +function fail () { + echo 1>&2 "${RED} $*" + exit 1 +} + +function sandbox () { + sandbox_tempdir="$(mktemp -t sandbox.XXXXXX -d)" + # shellcheck disable=2064 + trap "popd >/dev/null" EXIT + pushd "$sandbox_tempdir" >/dev/null \ + || fail "Could not change directory into temporary directory." + + local custom_init="${1:-}" + if [ -n "$custom_init" ]; then + eval "$custom_init" + fi +} + +function expect_equals () { + expect_run 0 test "${1:?}" = "${2:?}" +} + +function expect_exists () { + expect_run 0 test -e "${1:?}" +} + +function expect_run_sh () { + expect_run "${1:?}" bash -c "${2:?}" +} + +function expect_snapshot () { + local expected=${1:?} + local actual=${2:?} + if ! [ -e "$expected" ]; then + mkdir -p "${expected%/*}" + cp -R "$actual" "$expected" + fi + expect_run 0 diff -r -N "$expected" "$actual" +} + +function expect_run () { + local expected_exit_code=$1 + shift + local output= + set +e + output="$("$@" 2>&1)" + + local actual_exit_code=$? + if [[ "$actual_exit_code" == "$expected_exit_code" ]]; then + if [[ -n "${WITH_SNAPSHOT-}" ]]; then + local expected="$WITH_SNAPSHOT" + if ! [ -f "$expected" ]; then + mkdir -p "${expected%/*}" + echo -n "$output" > "$expected" || exit 1 + fi + if ! diff "$expected" <(echo -n "$output"); then + echo 1>&2 "${RED} - FAIL" + echo 1>&2 "${WHITE}\$ $*" + echo 1>&2 "Output snapshot did not match snapshot at '$expected'" + echo 1>&2 "$output" + exit 1 + fi + fi + echo 1>&2 + else + echo 1>&2 "${RED} - FAIL" + echo 1>&2 "${WHITE}\$ $*" + echo 1>&2 "${RED}Expected actual status $actual_exit_code to be $expected_exit_code" + echo 1>&2 "$output" + exit 1 + fi + set -e +}