Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e9a4472

Browse files
author
Sebastian Thiel
committed
First instantiation of template
0 parents  commit e9a4472

10 files changed

Lines changed: 344 additions & 0 deletions

File tree

.editorconfig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
11+
[*.sh]
12+
indent_style = space
13+
indent_size = 2
14+
15+
# Tab indentation (no size specified)
16+
[Makefile]
17+
indent_style = tab
18+
19+
# Matches the exact files either package.json or .travis.yml
20+
[{package.json,.travis.yml}]
21+
indent_style = space
22+
indent_size = 2

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Rust template
3+
# Generated by Cargo
4+
# will have compiled files and executables
5+
/target/
6+
7+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
8+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
9+
Cargo.lock
10+
11+
# These are backup files generated by rustfmt
12+
**/*.rs.bk
13+
14+
/.idea/
15+
16+
/callgrind.profile

Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "dua"
3+
version = "1.0.0"
4+
authors = ["Sebastian Thiel <[email protected]>"]
5+
publish = false
6+
edition = "2018"
7+
8+
[dependencies]
9+
failure = "0.1.1"
10+
failure-tools = "4.0.2"
11+
structopt = "0.2.14"
12+
13+
[[bin]]
14+
name="dua"
15+
path="src/main.rs"
16+
17+
[profile.release]
18+
panic = 'unwind'
19+
incremental = false
20+
overflow-checks = true

Makefile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
docker_image = docker_developer_environment
2+
3+
help:
4+
$(info -Targets -----------------------------------------------------------------------------)
5+
$(info -Development Targets -----------------------------------------------------------------)
6+
$(info lint | run lints with clippy)
7+
$(info benchmark | just for fun, really)
8+
$(info profile | only on linux - run callgrind and annotate it)
9+
$(info journey-tests | run all stateless journey test)
10+
$(info continuous-journey-tests | run all stateless journey test whenever something changes)
11+
$(info -- Use docker for all dependencies - run make interactively from there ----------------)
12+
$(info interactive-developer-environment-in-docker | gives you everything you need to run all targets)
13+
14+
always:
15+
16+
interactive-developer-environment-in-docker:
17+
docker build -t $(docker_image) - < etc/developer.Dockerfile
18+
docker run -v $$PWD:/volume -w /volume -it $(docker_image)
19+
20+
target/debug/dua: always
21+
cargo build
22+
23+
target/release/dua: always
24+
cargo build --release
25+
26+
lint:
27+
cargo clippy
28+
29+
profile: target/release/dua
30+
valgrind --callgrind-out-file=callgrind.profile --tool=callgrind $< >/dev/null
31+
callgrind_annotate --auto=yes callgrind.profile
32+
33+
benchmark: target/release/dua
34+
hyperfine '$<'
35+
36+
journey-tests: target/debug/dua
37+
./tests/stateless-journey.sh $<
38+
39+
continuous-journey-tests:
40+
watchexec $(MAKE) journey-tests
41+

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Getting Started
2+
3+
Find-and-replace the term `dua` with the name of your command-line application.
4+
5+

etc/developer.Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from guangie88/rustfmt-clippy:nightly
2+
3+
run cargo install hyperfine watchexec
4+
5+
run apt-get update
6+
run apt-get install -y valgrind
7+
8+
env PATH=$PATH:/root/.cargo/bin

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extern crate failure;
2+
3+
use failure::Error;
4+
5+
pub fn fun() -> Result<(), Error> {
6+
unimplemented!();
7+
}

src/main.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
extern crate failure;
2+
extern crate failure_tools;
3+
extern crate dua;
4+
#[macro_use]
5+
extern crate structopt;
6+
7+
use failure::Error;
8+
use failure_tools::ok_or_exit;
9+
use structopt::StructOpt;
10+
11+
mod options {
12+
use std::path::PathBuf;
13+
14+
#[derive(Debug, StructOpt)]
15+
#[structopt(name = "example", about = "An example of StructOpt usage.")]
16+
pub struct Args {
17+
/// Activate debug mode
18+
#[structopt(short = "d", long = "debug")]
19+
debug: bool,
20+
/// Set speed
21+
#[structopt(short = "s", long = "speed", default_value = "42")]
22+
speed: f64,
23+
/// Input file
24+
#[structopt(parse(from_os_str))]
25+
input: PathBuf,
26+
/// Output file, stdout if not present
27+
#[structopt(parse(from_os_str))]
28+
output: Option<PathBuf>,
29+
}
30+
}
31+
32+
fn run() -> Result<(), Error> {
33+
let opt = options::Args::from_args();
34+
println!("{:?}", opt);
35+
dua::fun()
36+
}
37+
38+
fn main() {
39+
ok_or_exit(run())
40+
}

tests/stateless-journey.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
set -eu
3+
4+
exe=${1:?First argument must be the executable to test}
5+
6+
root="$(cd "${0%/*}" && pwd)"
7+
# shellcheck disable=1090
8+
source "$root/utilities.sh"
9+
snapshot="$root/snapshots"
10+
11+
SUCCESSFULLY=0
12+
WITH_FAILURE=1
13+
14+
(with "no input file"
15+
it "fails with an error message" && {
16+
WITH_SNAPSHOT="$snapshot/failure-missing-input-file" \
17+
expect_run ${WITH_FAILURE} "$exe"
18+
}
19+
)
20+
(with "a valid input file"
21+
it "produces the expected output" && {
22+
WITH_SNAPSHOT="$snapshot/success-input-file-produces-correct-output" \
23+
expect_run ${SUCCESSFULLY} "$exe" <(echo this is probably not what you want)
24+
}
25+
)

tests/utilities.sh

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#!/bin/bash
2+
3+
WHITE="$(tput setaf 9 2>/dev/null || echo -n '')"
4+
YELLOW="$(tput setaf 3 2>/dev/null || echo -n '')"
5+
GREEN="$(tput setaf 2 2>/dev/null || echo -n '')"
6+
RED="$(tput setaf 1 2>/dev/null || echo -n '')"
7+
OFFSET=( )
8+
STEP=" "
9+
10+
function with_program () {
11+
local program="${1:?}"
12+
hash "$program" &>/dev/null || {
13+
function expect_run () {
14+
echo 1>&2 "${WHITE} - skipped (missing program)"
15+
}
16+
function expect_run_sh () {
17+
echo 1>&2 "${WHITE} - skipped (missing program)"
18+
}
19+
}
20+
}
21+
22+
function title () {
23+
echo "$WHITE-----------------------------------------------------"
24+
echo "${GREEN}$*"
25+
echo "$WHITE-----------------------------------------------------"
26+
}
27+
28+
function _context () {
29+
local name="${1:?}"
30+
shift
31+
echo 1>&2 "${YELLOW}${OFFSET[*]:-}[$name] $*"
32+
OFFSET+=("$STEP")
33+
}
34+
35+
function step () {
36+
_note step "${WHITE}" "$*"
37+
}
38+
39+
function stepn () {
40+
step "$*" $'\n'
41+
}
42+
43+
function with () {
44+
_context with "$*"
45+
}
46+
47+
function when () {
48+
_context when "$*"
49+
}
50+
51+
function _note () {
52+
local name="${1:?}"
53+
local color="${2:-}"
54+
shift 2
55+
echo 1>&2 -n "${OFFSET[*]:-}${color}[$name] ${*// /}"
56+
}
57+
58+
function it () {
59+
_note it "${GREEN}" "$*"
60+
}
61+
62+
function precondition () {
63+
_note precondition "${WHITE}" "$*"
64+
}
65+
66+
function shortcoming () {
67+
_note shortcoming "${RED}" "$*"
68+
}
69+
70+
function step () {
71+
_note step "${WHITE}" "$*"
72+
}
73+
74+
function stepn () {
75+
step "$*" $'\n'
76+
}
77+
78+
function fail () {
79+
echo 1>&2 "${RED} $*"
80+
exit 1
81+
}
82+
83+
function sandbox () {
84+
sandbox_tempdir="$(mktemp -t sandbox.XXXXXX -d)"
85+
# shellcheck disable=2064
86+
trap "popd >/dev/null" EXIT
87+
pushd "$sandbox_tempdir" >/dev/null \
88+
|| fail "Could not change directory into temporary directory."
89+
90+
local custom_init="${1:-}"
91+
if [ -n "$custom_init" ]; then
92+
eval "$custom_init"
93+
fi
94+
}
95+
96+
function expect_equals () {
97+
expect_run 0 test "${1:?}" = "${2:?}"
98+
}
99+
100+
function expect_exists () {
101+
expect_run 0 test -e "${1:?}"
102+
}
103+
104+
function expect_run_sh () {
105+
expect_run "${1:?}" bash -c -eu -o pipefail "${2:?}"
106+
}
107+
108+
function expect_snapshot () {
109+
local expected=${1:?}
110+
local actual=${2:?}
111+
if ! [ -e "$expected" ]; then
112+
mkdir -p "${expected%/*}"
113+
cp -R "$actual" "$expected"
114+
fi
115+
expect_run 0 diff -r -N "$expected" "$actual"
116+
}
117+
118+
function expect_run () {
119+
local expected_exit_code=$1
120+
shift
121+
local output=
122+
set +e
123+
if [[ -n "${SNAPSHOT_FILTER-}" ]]; then
124+
output="$("$@" 2>&1 | "$SNAPSHOT_FILTER")"
125+
else
126+
output="$("$@" 2>&1)"
127+
fi
128+
129+
local actual_exit_code=$?
130+
if [[ "$actual_exit_code" == "$expected_exit_code" ]]; then
131+
if [[ -n "${WITH_SNAPSHOT-}" ]]; then
132+
local expected="$WITH_SNAPSHOT"
133+
if ! [ -f "$expected" ]; then
134+
mkdir -p "${expected%/*}"
135+
echo -n "$output" > "$expected" || exit 1
136+
fi
137+
if ! diff "$expected" <(echo -n "$output"); then
138+
echo 1>&2 "${RED} - FAIL"
139+
echo 1>&2 "${WHITE}\$ $*"
140+
echo 1>&2 "Output snapshot did not match snapshot at '$expected'"
141+
echo 1>&2 "$output"
142+
if [ -n "${ON_ERROR:-}" ]; then
143+
eval "$ON_ERROR"
144+
fi
145+
exit 1
146+
fi
147+
fi
148+
echo 1>&2
149+
else
150+
echo 1>&2 "${RED} - FAIL"
151+
echo 1>&2 "${WHITE}\$ $*"
152+
echo 1>&2 "${RED}Expected actual status $actual_exit_code to be $expected_exit_code"
153+
echo 1>&2 "$output"
154+
if [ -n "${ON_ERROR:-}" ]; then
155+
eval "$ON_ERROR"
156+
fi
157+
exit 1
158+
fi
159+
set -e
160+
}

0 commit comments

Comments
 (0)