My New Hugo Site

  1. Bash
    1. Filters
      1. grep
      2. Sed
      3. jq
    2. Shellspec
  2. Design
  3. Architectural Patterns
    1. Systemd
    2. Message Broker
    3. JSON-RPC
  4. Go
    1. Concurrency
    2. Web Applications
    3. Compound Data
    4. Json
    5. Go vs Erlang
  5. Prolog Cookbook
  6. Documentation
    1. Hugo
      1. Go Html Template
      2. Table of Contents
    2. HTML
    3. CSS
      1. Color
      2. Style Guides
      3. Layout
    4. Mathjax
  7. Visualization
    1. D3
      1. Venn Diagrams
    2. SVG
    3. Visjs
      1. Network
  8. Data
    1. Yaml
    2. Events
      1. JSON-LD
    3. JSON
      1. jCal
    4. SQL
  9. JavaScript

Shellspec

Gerard Meszaros’s book

documentation

Project directory

.
├── .shellspec 
├── lib
│   └── hello.sh
└── spec
    ├── hello_spec.sh
    └── spec_helper.sh

A .shellspec file is required in the project directory. It can be created with shellspec --init.

--require spec_helper

## Default kcov (coverage) options
# --kcov-options "--include-path=. --path-strip-level=1"
# --kcov-options "--include-pattern=.sh"
# --kcov-options "--exclude-pattern=/.shellspec,/spec/,/coverage/,/report/"

## Example: Include script "myprog" with no extension
# --kcov-options "--include-pattern=.sh,myprog"

## Example: Only specified files/directories
# --kcov-options "--include-pattern=myprog,/lib/"

Running shellcheck -s bash in the above . directory:

$ shellspec
Running: /bin/sh [bash 5.2.21(1)-release]
.

Finished in 0.43 seconds (user 0.31 seconds, sys 0.09 seconds)
1 example, 0 failures

./lib/hello.sh

#!/usr/bin/bash

hello() {
  echo "Hello ${1}!"
}

./spec/hello_spec.sh

Describe 'hello.sh'
  Include lib/hello.sh
  It 'says hello'
    When call hello ShellSpec
    The output should equal 'Hello ShellSpec!'
  End
End

Examples of _spec.sh files are in https://github.com/shellspec/shellspec/tree/master/examples/spec

ShellSpec has its own DSL to write tests. It may seem like a distinctive code because DSL starts with a capital letter, but the syntax is compatible with shell scripts, and you can embed shell functions and use ShellCheck to check the syntax.

Basic Structure

ExampleGroup
A block for grouping example groups or examples.
Describe
An alias for ExampleGroup.
Context
An alias for ExampleGroup.
Example
A block for writing an evaluation and expectations.
It
An alias for Example
Specify
An alias for Example
When
call or run a shell function or command.
The
Expectation begins with The which does the verification.
BeforeEach
AfterEach
BeforeAll
AfterAll
Include
Include a shell script to test.
Set
Set shell options before executing each example.
Data
The input data is specified after #| in the Data or Data:expand block.
Parameters
defines a block of parameters.

Evaluation

./spec/spec_helper.sh

# shellcheck shell=sh

# Defining variables and functions here will affect all specfiles.
# Change shell options inside a function may cause different behavior,
# so it is better to set them here.
# set -eu

# This callback function will be invoked only once before loading specfiles.
spec_helper_precheck() {
  # Available functions: info, warn, error, abort, setenv, unsetenv
  # Available variables: VERSION, SHELL_TYPE, SHELL_VERSION
  : minimum_version "0.29.0"
}

# This callback function will be invoked after a specfile has been loaded.
spec_helper_loaded() {
  :
}

# This callback function will be invoked after core modules has been loaded.
spec_helper_configure() {
  # Available functions: import, before_each, after_each, before_all, after_all
  : import 'support/custom_matcher'
}

Four-Phase Test

  1. set up the test fixture
  2. Call system under test (SUT)
  3. Was expected outcome obtained?
  4. teardown the test fixture