Skip to content

Commit

Permalink
Merge pull request #106 from marcransome/check-command
Browse files Browse the repository at this point in the history
Add check command
  • Loading branch information
marcransome authored Mar 19, 2022
2 parents 0193ddc + 50a5e2a commit 9637142
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Use the `status` command without arguments to view the global status of all pond

```console
$ pond status
● pond 2.1.0
● pond 2.2.0
Health: good
Ponds: 1 (1 enabled; 1 loaded)
Loaded: /root/.config/fish/pond
Expand Down
3 changes: 2 additions & 1 deletion completions/pond.fish
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
set -l commands_without_options_single_pond status dir config autoload autounload
set -l commands_without_options_multiple_pond load unload enable disable
set -l commands_without_options_multiple_pond load unload enable disable check
set -l commands_with_options remove drain list
set -l commands "create $commands_without_options_single_pond $commands_without_options_multiple_pond $commands_with_options"

Expand All @@ -12,6 +12,7 @@ complete -c pond -n "not __fish_seen_subcommand_from $commands" -a 'autoload' -d
complete -c pond -n "not __fish_seen_subcommand_from $commands" -a 'autounload' -d 'Create or edit pond autounload function'
complete -c pond -n "not __fish_seen_subcommand_from $commands" -a 'remove' -d 'Remove a pond and associated data'
complete -c pond -n "not __fish_seen_subcommand_from $commands" -a 'list' -d 'List ponds'
complete -c pond -n "not __fish_seen_subcommand_from $commands" -a 'check' -d 'Check pond functions for syntax issues'
complete -c pond -n "not __fish_seen_subcommand_from $commands" -a 'enable' -d 'Enable a pond for new shell sessions'
complete -c pond -n "not __fish_seen_subcommand_from $commands" -a 'disable' -d 'Disable a pond for new shell sessions'
complete -c pond -n "not __fish_seen_subcommand_from $commands" -a 'load' -d 'Load pond into current shell session'
Expand Down
66 changes: 64 additions & 2 deletions functions/pond.fish
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function pond -a command -d "A fish shell environment manager"
set -g pond_version 2.1.0
set -g pond_version 2.2.0

function __pond_usage
echo "\
Expand All @@ -22,6 +22,7 @@ Commands:
autounload Create or edit pond autounload function
remove Remove a pond and associated data
list List ponds
check Check pond functions for syntax issues
enable Enable a pond for new shell sessions
disable Disable a pond for new shell sessions
load Load pond into current shell session
Expand Down Expand Up @@ -146,6 +147,16 @@ Arguments:
echo
end

function __pond_check_command_usage
echo "\
Usage:
pond check ponds...
Arguments:
ponds The name of one or more ponds to check" >&2
echo
end

function __pond_drain_command_usage
echo "\
Usage:
Expand Down Expand Up @@ -449,7 +460,38 @@ Usage:
echo (string pad -w $pad_width "Autounload:") (test -f $pond_path/{$pond_name}_{$pond_autounload_suffix}.fish; and echo "present"; or echo "absent")
echo (string pad -w $pad_width "Functions:") (count $pond_path/**.fish)
echo (string pad -w $pad_width "Size:") (string trim (du -sh $pond_path | cut -f 1))
end

function __pond_check_operation -a pond_name
set -l pond_path $pond_home/$pond_name
set -l passes 0
set -l failures 0

echo "Checking pond '$pond_name' for syntax issues.."

for pond_function in $pond_path/**.fish
echo -n " "
if test (fish --no-execute $pond_function 2>/dev/null 1>&2) $status -eq 0
set_color green; and echo -n ""; and set_color normal
set passes (math $passes + 1)
else
set_color red; and echo -n ""; and set_color normal
set failures (math $failures + 1)
end

echo " "(basename $pond_function)
end

set -l total (math $passes + $failures)

set_color green; and echo -n "passed: $passes"; and set_color normal
echo -n " "
set_color red; and echo -n "failed: $failures"; and set_color normal
echo " of $total functions"

if test $failures -ne 0
return 1
end
end

function __pond_drain_operation -a pond_name
Expand Down Expand Up @@ -510,7 +552,7 @@ Usage:
function __pond_cleanup
functions -e __pond_cleanup

for command in create remove list enable disable load unload status drain dir config
for command in create remove list enable disable load unload status check drain dir config
functions -e "__pond_"(echo $command)"_command_usage"
functions -e "__pond_"(echo $command)"_operation"
end
Expand Down Expand Up @@ -720,6 +762,26 @@ Usage:
__pond_cleanup; and return $exit_code
end
end
case check
if test (count $argv) -eq 0; __pond_check_command_usage; and __pond_cleanup; and return 1; end

for pond_name in $argv
if ! __pond_name_is_valid "$pond_name"
__pond_check_command_usage
__pond_cleanup
return 1
else if ! __pond_exists $pond_name
__pond_show_not_exists_error $pond_name
__pond_cleanup
return 1
end

__pond_check_operation $pond_name
set -l exit_code $status
if test $exit_code -gt 0
__pond_cleanup; and return $exit_code
end
end
case drain
if ! argparse 'y/yes' >/dev/null 2>&1 -- $argv
__pond_drain_command_usage
Expand Down
29 changes: 27 additions & 2 deletions manpages/pond.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
.\" Automatically generated by Pandoc 2.17.0.1
.\" Automatically generated by Pandoc 2.17.1.1
.\"
.TH "pond" "1" "" "Version 2.1.0" "Pond User\[cq]s Guide"
.\" Define V font for inline verbatim, using C font in formats
.\" that render this, and otherwise B font.
.ie "\f[CB]x\f[]"x" \{\
. ftr V B
. ftr VI BI
. ftr VB B
. ftr VBI BI
.\}
.el \{\
. ftr V CR
. ftr VI CI
. ftr VB CB
. ftr VBI CBI
.\}
.TH "pond" "1" "" "Version 2.2.0" "Pond User\[cq]s Guide"
.hy
.SH NAME
.PP
Expand Down Expand Up @@ -94,6 +108,17 @@ only)
.PP
\f[I]Example:\f[R] \f[B]pond list --enabled --unloaded\f[R] (list
enabled \f[I]and\f[R] unloaded ponds only)
.SS \f[B]check\f[R] \f[I]ponds\&...\f[R]
.PP
Check \f[I]ponds\f[R] for syntax issues.
A list of function filenames will be printed, one filename per line, for
each named pond.
Function filenames will be prefixed with a green tick or red cross,
indicating syntax validation success or failure for that file.
A trailing line will be printed with the total number of passes,
failures, and total function count for the pond.
This command will exit early if syntax issues are found, after the first
pond with failures.
.SS \f[B]autoload\f[R] \f[I]pond\f[R]
.PP
Open the autoload function for \f[I]pond\f[R] in an interactive editor.
Expand Down
21 changes: 19 additions & 2 deletions manpages/pond.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>pond(1) Version 2.1.0 | Pond User’s Guide</title>
<title>pond(1) Version 2.2.0 | Pond User’s Guide</title>
<style>
html {
line-height: 1.5;
Expand All @@ -30,6 +30,9 @@
font-size: 0.9em;
padding: 1em;
}
h1 {
font-size: 1.8em;
}
}
@media print {
body {
Expand Down Expand Up @@ -134,6 +137,12 @@
#TOC li {
list-style: none;
}
#TOC ul {
padding-left: 1.3em;
}
#TOC > ul {
padding-left: 0;
}
#TOC a:not(:hover) {
text-decoration: none;
}
Expand All @@ -151,7 +160,7 @@
</head>
<body>
<header id="title-block-header">
<h1 class="title">pond(1) Version 2.1.0 | Pond User’s Guide</h1>
<h1 class="title">pond(1) Version 2.2.0 | Pond User’s Guide</h1>
</header>
<h1 id="name">NAME</h1>
<p><strong>pond</strong> — a shell environment manager for the fish
Expand Down Expand Up @@ -244,6 +253,14 @@ <h2 id="remove--y--yes-ponds"><strong>remove</strong>
disabled ponds only)</p>
<p><em>Example:</em> <strong>pond list --enabled --unloaded</strong>
(list enabled <em>and</em> unloaded ponds only)</p>
<h2 id="check-ponds"><strong>check</strong> <em>ponds…</em></h2>
<p>Check <em>ponds</em> for syntax issues. A list of function filenames
will be printed, one filename per line, for each named pond. Function
filenames will be prefixed with a green tick or red cross, indicating
syntax validation success or failure for that file. A trailing line will
be printed with the total number of passes, failures, and total function
count for the pond. This command will exit early if syntax issues are
found, after the first pond with failures.</p>
<h2 id="autoload-pond"><strong>autoload</strong> <em>pond</em></h2>
<p>Open the autoload function for <em>pond</em> in an interactive
editor. The function will be created if it does not already exist and
Expand Down
11 changes: 8 additions & 3 deletions manpages/pond.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% pond(1) Version 2.1.0 | Pond User's Guide
% pond(1) Version 2.2.0 | Pond User's Guide

NAME
====
Expand Down Expand Up @@ -81,8 +81,13 @@ _Example:_ **pond list \--disabled** (list disabled ponds only)

_Example:_ **pond list \--enabled \--unloaded** (list enabled _and_ unloaded ponds only)

**check** _ponds..._
--------------------

Check _ponds_ for syntax issues. A list of function filenames will be printed, one filename per line, for each named pond. Function filenames will be prefixed with a green tick or red cross, indicating syntax validation success or failure for that file. A trailing line will be printed with the total number of passes, failures, and total function count for the pond. This command will exit early if syntax issues are found, after the first pond with failures.

**autoload** _pond_
---------------
-------------------

Open the autoload function for _pond_ in an interactive editor. The function will be created if it does not already exist and will be named after _pond_ with the suffix \_autoload.

Expand All @@ -91,7 +96,7 @@ If _pond_ is enabled (see **enable**) the function will be executed automaticall
See **ENVIRONMENT** for a discussion of the **pond\_editor** _universal_ variable that controls which editor is used when this command is invoked.

**autounload** _pond_
-----------------
---------------------

Open the autounload function for _pond_ in an interactive editor. The function will be created if it does not already exist and will be named after _pond_ with the suffix \_autounload.

Expand Down
120 changes: 120 additions & 0 deletions tests/check.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
set parent (dirname (status --current-filename))
source $parent/fixtures/functions.fish
source $parent/fixtures/variables.fish

set command_usage "\
Usage:
pond check ponds...
Arguments:
ponds The name of one or more ponds to check"

set not_exists_error (__pond_error_string "Pond does not exist: no-exist")

function __pond_syntax_success_string_autoload -a pond_name
echo -n " "; set_color green; and echo -n ""; and set_color normal; and echo " "{$pond_name}_autoload.fish;
end

function __pond_syntax_success_string_autounload -a pond_name
echo -n " "; set_color green; and echo -n ""; and set_color normal; and echo " "{$pond_name}_autounload.fish;
end

function __pond_syntax_failure_string_autoload -a pond_name
echo -n " "; set_color red; and echo -n ""; and set_color normal; and echo " "{$pond_name}_autoload.fish;
end

function __pond_syntax_failure_string_autounload -a pond_name
echo -n " "; set_color red; and echo -n ""; and set_color normal; and echo " "{$pond_name}_autounload.fish;
end

function __pond_syntax_totals_string -a passed failed total
set_color green; and echo -n "passed: $passed";
and set_color normal; and echo -n " ";
and set_color red; and echo -n "failed: $failed";
and set_color normal; and echo " of $total functions"
end

set success_output_single_pond "\
Checking pond '$pond_name' for syntax issues..
"(__pond_syntax_success_string_autoload $pond_name)"
"(__pond_syntax_success_string_autounload $pond_name)"
"(__pond_syntax_totals_string 2 0 2)

set success_output_multiple_ponds "\
Checking pond '$pond_name_prefix-1' for syntax issues..
"(__pond_syntax_success_string_autoload $pond_name_prefix-1)"
"(__pond_syntax_success_string_autounload $pond_name_prefix-1)"
"(__pond_syntax_totals_string 2 0 2)"
Checking pond '$pond_name_prefix-2' for syntax issues..
"(__pond_syntax_success_string_autoload $pond_name_prefix-2)"
"(__pond_syntax_success_string_autounload $pond_name_prefix-2)"
"(__pond_syntax_totals_string 2 0 2)"
Checking pond '$pond_name_prefix-3' for syntax issues..
"(__pond_syntax_success_string_autoload $pond_name_prefix-3)"
"(__pond_syntax_success_string_autounload $pond_name_prefix-3)"
"(__pond_syntax_totals_string 2 0 2)

set failure_output_single_pond "\
Checking pond '$pond_name' for syntax issues..
"(__pond_syntax_failure_string_autoload $pond_name)"
"(__pond_syntax_failure_string_autounload $pond_name)"
"(__pond_syntax_totals_string 0 2 2)

# The 'check' subcommand exits on the first pond with syntax issues
set failure_output_multiple_ponds "\
Checking pond '$pond_name_prefix-1' for syntax issues..
"(__pond_syntax_failure_string_autoload $pond_name_prefix-1)"
"(__pond_syntax_failure_string_autounload $pond_name_prefix-1)"
"(__pond_syntax_totals_string 0 2 2)

@echo "pond check: success tests for single pond with valid autoload/autounload functions"
__pond_setup 1 enabled loaded populated
@test "pond check: success exit code" (pond check $pond_name >/dev/null 2>&1) $status -eq $success
__pond_tear_down

@echo "pond check: success output tests for single pond with valid autoload/autounload functions"
__pond_setup 1 enabled loaded populated
@test "pond check: success output message" (pond check $pond_name 2>&1 | string collect) = $success_output_single_pond
__pond_tear_down

__pond_setup 1 enabled loaded populated
@echo "pond check: failure tests for single pond with invalid autoload/autounload functions"
echo "end" > $pond_home/$pond_name/{$pond_name}_autoload.fish
echo "end" > $pond_home/$pond_name/{$pond_name}_autounload.fish
@test "setup: invalid autoload function definition" (cat $pond_home/$pond_name/{$pond_name}_autoload.fish | string collect) = "end"
@test "setup: invalid autounload function definition" (cat $pond_home/$pond_name/{$pond_name}_autounload.fish | string collect) = "end"
@test "pond check: failure exit code" (pond check $pond_name >/dev/null 2>&1) $status -eq $failure

@echo "pond check: failure output tests for single pond with invalid autoload/autounload functions"
@test "pond check: failure output message" (pond check $pond_name 2>&1 | string collect) = $failure_output_single_pond
__pond_tear_down

__pond_setup 3 enabled loaded populated
@echo "pond check: success tests for multiple ponds with valid autoload/autounload functions"
@test "pond check: success exit code" (pond check $pond_name_prefix-1 $pond_name_prefix-2 $pond_name_prefix-3 >/dev/null 2>&1) $status -eq $success

@echo "pond check: success output tests for multiple ponds with valid autoload/autounload functions"
@test "pond check: success output message" (pond check $pond_name_prefix-1 $pond_name_prefix-2 $pond_name_prefix-3 2>&1 | string collect) = $success_output_multiple_ponds
__pond_tear_down

__pond_setup 3 enabled loaded populated
@echo "pond check: failure tests for multiple ponds with invalid autoload/autounload functions"
echo "end" > $pond_home/$pond_name_prefix-1/{$pond_name_prefix-1}_autoload.fish
echo "end" > $pond_home/$pond_name_prefix-1/{$pond_name_prefix-1}_autounload.fish
@test "setup: $pond_name_prefix-1 invalid autoload function definition" (cat $pond_home/$pond_name_prefix-1/{$pond_name_prefix-1}_autoload.fish | string collect) = "end"
@test "setup: $pond_name_prefix-1 invalid autounload function definition" (cat $pond_home/$pond_name_prefix-1/{$pond_name_prefix-1}_autounload.fish | string collect) = "end"
@test "pond check: failure exit code" (pond check $pond_name_prefix-1 $pond_name_prefix-2 $pond_name_prefix-3 >/dev/null 2>&1) $status -eq $failure

@echo "pond check: failure output tests for multiple ponds with invalid autoload/autounload functions"
@test "pond check: failure output message" (pond check $pond_name_prefix-1 $pond_name_prefix-2 $pond_name_prefix-3 2>&1 | string collect) = $failure_output_multiple_ponds
__pond_tear_down

@echo "pond check: validation failure exit code tests"
@test "pond check: fails for missing pond name" (pond check >/dev/null 2>&1) $status -eq $failure
@test "pond check: fails for malformed pond name" (pond check _invalid >/dev/null 2>&1) $status -eq $failure
@test "pond check: fails for non-existent pond" (pond check no-exist >/dev/null 2>&1) $status -eq $failure

@echo "pond check: validation failure output tests"
@test "pond check: command usage shown for missing pond name" (pond check 2>&1 | string collect) = $command_usage
@test "pond check: command usage shown for malformed pond name" (pond check _invalid 2>&1 | string collect) = $command_usage
@test "pond check: command error shown for non-existent pond" (pond check no-exist 2>&1 | string collect) = $not_exists_error
1 change: 1 addition & 0 deletions tests/options.fish
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Commands:
autounload Create or edit pond autounload function
remove Remove a pond and associated data
list List ponds
check Check pond functions for syntax issues
enable Enable a pond for new shell sessions
disable Disable a pond for new shell sessions
load Load pond into current shell session
Expand Down

0 comments on commit 9637142

Please sign in to comment.