Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add import function; extend tests; modify workflows #6

Merged
merged 9 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/argsh.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Release
name: argsh
on:
push:
branches:
Expand Down Expand Up @@ -43,6 +43,10 @@ jobs:
- name: Coverage
run: |
argsh coverage
- name: Is insync
run: |
git diff --exit-code \
--ignore-matching-lines '^ "date":'

minify:
runs-on: ubuntu-latest
Expand Down
6 changes: 1 addition & 5 deletions .github/workflows/docs.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Documentation Tests
name: docs
on:
pull_request:
paths:
Expand All @@ -14,7 +14,6 @@ concurrency:

jobs:
docs-test:
if: ${{ startsWith(github.head_ref, 'docs/') }}
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -39,7 +38,6 @@ jobs:
run: yarn build:docs

vale-docs:
if: ${{ startsWith(github.head_ref, 'docs/') }}
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -65,7 +63,6 @@ jobs:
REVIEWDOG_GITHUB_API_TOKEN: ${{ github.token }}

content-eslint:
if: ${{ startsWith(github.head_ref, 'docs/') }}
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -82,7 +79,6 @@ jobs:
run: yarn lint:content

code-docs-eslint:
if: ${{ startsWith(github.head_ref, 'docs/') }}
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
12 changes: 2 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,12 @@ If you, as a community contributor, wish to work on more extensive features, ple

1. Before you start working on a change please make sure that there is an issue for what you will be working on. You can either find and [existing issue](https://github.com/arg-sh/argsh/issues) or [open a new issue](https://github.com/arg-sh/argsh/issues/new) if none exists. Doing this makes sure that others can contribute with thoughts or suggest alternatives, ultimately making sure that we only add changes that make

2. When you are ready to start working on a change you should first [fork the argsh repo](https://help.github.com/en/github/getting-started-with-github/fork-a-repo) and [branch out](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-and-deleting-branches-within-your-repository) from the `develop` branch.
2. When you are ready to start working on a change you should first [fork the argsh repo](https://help.github.com/en/github/getting-started-with-github/fork-a-repo) and [branch out](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-and-deleting-branches-within-your-repository) from the `main` branch.
3. Make your changes.
4. [Open a pull request towards the develop branch in the argsh repo](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request-from-a-fork). Within a couple of days a argsh team member will review, comment and eventually approve your PR.

## Workflow

### Branches

All changes should be part of a branch and submitted as a pull request - your branches should be prefixed with one of:

- `fix/` for bug fixes
- `feat/` for features
- `docs/` for documentation changes

### Commits

Strive towards keeping your commits small and isolated - this helps the reviewer understand what is going on and makes it easier to process your requests.
Expand All @@ -41,7 +33,7 @@ Strive towards keeping your commits small and isolated - this helps the reviewer

If your changes should result in a new version of argsh, you will need to generate a **changelog**. Follow [this guide](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md) on how to generate a changeset.

Finally, submit your branch as a pull request. Your pull request should be opened against the `develop` branch in the main argsh repo.
Finally, submit your branch as a pull request. Your pull request should be opened against the `main` branch in the main argsh repo.

In your PR's description you should follow the structure:

Expand Down
18 changes: 9 additions & 9 deletions argsh.min.sh

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion argsh.min.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
# shellcheck disable=SC2178 disable=SC2120 disable=SC1090 disable=SC2046 disable=SC2155
ARGSH_COMMIT_SHA="${commit_sha}"; ARGSH_VERSION="${version}"
set -euo pipefail; ARGSH_COMMIT_SHA="${commit_sha}"; ARGSH_VERSION="${version}"
${data};[[ "${BASH_SOURCE[0]}" != "${0}" ]] || argsh::shebang "${@}"
12 changes: 7 additions & 5 deletions coverage/coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
"files": [
{"file": "/workspace/test/fixtures/args/attrs.sh", "percent_covered": "52.50", "covered_lines": "21", "total_lines": "40"},
{"file": "/workspace/libraries/array.sh", "percent_covered": "35.71", "covered_lines": "5", "total_lines": "14"},
{"file": "/workspace/test/fixtures/import/print_out.sh", "percent_covered": "100.00", "covered_lines": "2", "total_lines": "2"},
{"file": "/workspace/libraries/error.sh", "percent_covered": "27.78", "covered_lines": "5", "total_lines": "18"},
{"file": "/workspace/test/fixtures/args/fmt.sh", "percent_covered": "33.33", "covered_lines": "13", "total_lines": "39"},
{"file": "/workspace/libraries/to.sh", "percent_covered": "41.67", "covered_lines": "10", "total_lines": "24"},
{"file": "/workspace/libraries/is.sh", "percent_covered": "75.00", "covered_lines": "6", "total_lines": "8"},
{"file": "/workspace/libraries/fmt.sh", "percent_covered": "62.50", "covered_lines": "5", "total_lines": "8"},
{"file": "/workspace/libraries/string.sh", "percent_covered": "43.90", "covered_lines": "18", "total_lines": "41"},
{"file": "/workspace/libraries/import.sh", "percent_covered": "100.00", "covered_lines": "19", "total_lines": "19"},
{"file": "/workspace/libraries/string.sh", "percent_covered": "100.00", "covered_lines": "41", "total_lines": "41"},
{"file": "/workspace/test/fixtures/args/usage.sh", "percent_covered": "67.57", "covered_lines": "25", "total_lines": "37"},
{"file": "/workspace/libraries/args.sh", "percent_covered": "89.80", "covered_lines": "273", "total_lines": "304"}
],
"percent_covered": "71.48",
"covered_lines": 381,
"total_lines": 533,
"percent_covered": "76.71",
"covered_lines": 425,
"total_lines": 554,
"percent_low": 25,
"percent_high": 75,
"command": "bats",
"date": "2024-03-23 12:57:16"
"date": "2024-03-25 12:26:58"
}
12 changes: 5 additions & 7 deletions libraries/bash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@ bash::version() {
local major="${1:-4}"
local minor="${2:-3}"
local patch="${3:-0}"
local -a version
read -ra version <<< "$(echo "${BASH_VERSION}" | tr '.' ' ')"

if [[ "${version[0]}" -lt "${major}" ]]; then
if [[ "${BASH_VERSINFO[0]}" -lt "${major}" ]]; then
return 1
elif [[ "${version[0]}" -gt "${major}" ]]; then
elif [[ "${BASH_VERSINFO[0]}" -gt "${major}" ]]; then
return 0
fi

if [[ "${version[1]}" -lt "${minor}" ]]; then
if [[ "${BASH_VERSINFO[1]}" -lt "${minor}" ]]; then
return 1
elif [[ "${version[1]}" -gt "${minor}" ]]; then
elif [[ "${BASH_VERSINFO[1]}" -gt "${minor}" ]]; then
return 0
fi

if [[ "${version[2]}" -lt "${patch}" ]]; then
if [[ "${BASH_VERSINFO[2]}" -lt "${patch}" ]]; then
return 1
fi

Expand Down
72 changes: 72 additions & 0 deletions libraries/import.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env bats
# shellcheck shell=bash disable=SC2154
# vim: filetype=bash
set -euo pipefail

load ../test/helper
load_source

@test "can import library" {
(
import "string"
string::random
) >"${stdout}" 2>"${stderr}" || status="${?}"

if [[ "${BATS_LOAD}" == "argsh.min.sh" ]]; then
assert "${status}" -eq 1
is_empty stdout
contains "^Library not found argsh.min.sh/string" stderr
else
assert "${status}" -eq 0
is_empty stderr
not_empty stdout
fi
}

@test "can import from @" {
(
# shellcheck disable=SC2030
export PATH_BASE="${PATH_FIXTURES}"
import "@print_out"
) >"${stdout}" 2>"${stderr}" || status="${?}"

assert "${status}" -eq 0
is_empty stderr
contains "^out" stdout
}

@test "can import from ~" {
(
unset ARGSH_SOURCE
import "~bats-format-cat"
) < <(echo "some input") >"${stdout}" 2>"${stderr}" || status="${?}"

assert "${status}" -eq 0
is_empty stderr
contains "^some input" stdout
}

@test "using import cache and can clear it" {
(
# shellcheck disable=SC2031
export PATH_BASE="${PATH_FIXTURES}"
import "@print_out"
import "@print_out"
import::clear
import "@print_out"
) >"${stdout}" 2>"${stderr}" || status="${?}"

assert "${status}" -eq 0
is_empty stderr
contains "^out\nout\n$" stdout
}

@test "errors when importing non-existent library" {
(
import "non-existent"
) >"${stdout}" 2>"${stderr}" || status="${?}"

assert "${status}" -eq 1
is_empty stdout
contains "^Library not found" stderr
}
51 changes: 51 additions & 0 deletions libraries/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash
# @file import
# @brief Import libraries
# @description
# This file contains functions for importing libraries
set -euo pipefail

declare -gA import_cache=()

# @description
# Import a library, relative to the current script
# If '@' is prepended to the library name, it will be imported from the base path
# If '~' is prepended to the library name, it will be imported from the script entry point
# @arg $1 string Library name
# @example
# import fmt
import() {
local src="${1}"
(( ${import_cache["${src}"]:-} )) || {
import_cache["${src}"]=1
# shellcheck disable=SC1090
if [[ ${src:0:1} == "@" ]]; then
src="${PATH_BASE:?"PATH_BASE missing"}/${src:1}";
elif [[ ${src:0:1} == "~" ]]; then
local _s="${ARGSH_SOURCE:-${BASH_SOURCE[-1]}}"
src="${_s%/*}/${src:1}"
else
src="${BASH_SOURCE[0]%/*}/${src}"
fi
import::source "${src}" || exit 1
}
}

import::source() {
local src="${1}"
for ext in "" ".sh" ".bash"; do
if [[ -f "${src}${ext}" ]]; then
# shellcheck disable=SC1090
. "${src}${ext}"
return
fi
done
echo "Library not found ${src}" >&2
return 1
}

# @description
# Clear the import cache
import::clear() {
import_cache=()
}
58 changes: 58 additions & 0 deletions libraries/string.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env bats
# shellcheck shell=bash disable=SC2154
# vim: filetype=bash
set -euo pipefail

load ../test/helper
load_source

@test "trim-left" {
result="$(string::trim-left " foo")"
assert "foo" = "${result}"
}

@test "trim-left with custom character" {
result="$(string::trim-left " x foo" "x ")"
assert "foo" = "${result}"
}

@test "trim-right" {
result="$(string::trim-right "foo ")"
assert "foo" = "${result}"
}

@test "trim-right with custom character" {
result="$(string::trim-right "foo x " "x ")"
assert "foo" = "${result}"
}

@test "trim" {
result="$(string::trim " foo ")"
assert "foo" = "${result}"
}

@test "trim with custom character" {
result="$(string::trim " x foo x " "x ")"
assert "foo" = "${result}"
}

@test "random" {
result="$(string::random)"
assert "${#result}" -eq 42
}

@test "random with length" {
result="$(string::random 10)"
assert "${#result}" -eq 10
}

@test "random with length and characters" {
result="$(string::random 10 "abc")"
assert "${#result}" -eq 10
assert "" = "${result//[abc]/}"
}

@test "drop-index" {
result="$(string::drop-index "hello" 1 2)"
assert "hlo" = "${result}"
}
4 changes: 4 additions & 0 deletions test/fixtures/import/print_out.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -euo pipefail

echo "out"
19 changes: 19 additions & 0 deletions test/helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ assert() {
return 1
}
}

is_empty() {
local check="${1}"
[[ -n "${!check}" ]] || return 0
Expand All @@ -72,6 +73,24 @@ is_empty() {
return 1
}

not_empty() {
local check="${1}"
[[ -n "${!check}" ]] || return 1
[[ -s "${!check}" ]] || return 1

return 0
}

contains() {
local check="${1}"
local -n file="${2}"
grep -qzP "${check}" "${file}" || {
echo "■■ ${file} does not contain ${check}"
cat "${file}"
return 1
}
}

is::uninitialized() {
local var
for var in "${@}"; do
Expand Down
2 changes: 1 addition & 1 deletion www/apps/docs/content/contribution/argsh.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ If you’re adding a new library or contributing to the codebase, you need to fo

When you make an edit to an existing documentation page or fork the repository to make changes to the documentation, you have to create a new branch.

Documentation contributions always use `develop` as the base branch. Make sure to also open your PR against the `develop` branch.
Documentation contributions always use `main` as the base branch. Make sure to also open your PR against the `main` branch.

### Branch Name

Expand Down
6 changes: 1 addition & 5 deletions www/apps/docs/content/contribution/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ If you’re adding a new page or contributing to the codebase, you need to fork

When you make an edit to an existing documentation page or fork the repository to make changes to the documentation, you have to create a new branch.

Documentation contributions always use `develop` as the base branch. Make sure to also open your PR against the `develop` branch.

### Branch Name

Make sure that the branch name starts with `docs/`. For example, `docs/fix-services`. Vercel deployed previews are only triggered for branches starting with `docs/`.
Documentation contributions always use `main` as the base branch. Make sure to also open your PR against the `main` branch.

### Pull Request Conventions

Expand Down
Loading