Skip to content

Commit

Permalink
Merge pull request #229 from marcransome/add-show-option
Browse files Browse the repository at this point in the history
Add -s/--show option to autoload/autounload commands
  • Loading branch information
marcransome authored Oct 3, 2024
2 parents d31360f + 3460601 commit 92c1692
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Use the `status` command without arguments to view the global status of all pond

```console
$ pond status
● pond 2.5.5
● pond 2.6.0
Health: good
Ponds: 1 (1 enabled; 1 loaded)
Loaded: /root/.config/fish/pond
Expand Down
2 changes: 1 addition & 1 deletion docs/pond.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.\" Automatically generated by Pandoc 2.14.0.3
.\"
.TH "pond" "1" "" "Version 2.5.5" "Pond User\[cq]s Guide"
.TH "pond" "1" "" "Version 2.6.0" "Pond User\[cq]s Guide"
.hy
.SH NAME
.PP
Expand Down
4 changes: 2 additions & 2 deletions docs/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.5.5 | Pond User’s Guide</title>
<title>pond(1) Version 2.6.0 | Pond User’s Guide</title>
<style>
html {
line-height: 1.5;
Expand Down Expand Up @@ -150,7 +150,7 @@
</head>
<body>
<header id="title-block-header">
<h1 class="title">pond(1) Version 2.5.5 | Pond User’s Guide</h1>
<h1 class="title">pond(1) Version 2.6.0 | Pond User’s Guide</h1>
</header>
<h1 id="name">NAME</h1>
<p><strong>pond</strong> — a shell environment manager for the fish shell</p>
Expand Down
2 changes: 1 addition & 1 deletion docs/pond.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% pond(1) Version 2.5.5 | Pond User's Guide
% pond(1) Version 2.6.0 | Pond User's Guide

NAME
====
Expand Down
82 changes: 65 additions & 17 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.5.5
set -g pond_version 2.6.0

function __pond_usage
echo "\
Expand Down Expand Up @@ -50,7 +50,10 @@ Arguments:
function __pond_autoload_command_usage
echo "\
Usage:
pond autoload <name>
pond autoload [options] <name>
Options:
-s, --show Show autoload function without opening editor
Arguments:
name The name of the pond for which an autoload function
Expand All @@ -62,7 +65,10 @@ Arguments:
function __pond_autounload_command_usage
echo "\
Usage:
pond autounload <name>
pond autounload [options] <name>
Options:
-s, --show Show autounload function without opening editor
Arguments:
name The name of the pond for which an autounload function
Expand Down Expand Up @@ -230,6 +236,17 @@ Usage:
$pond_editor $pond_autoload_file
end

function __pond_autoload_show_operation -a pond_name
set -l pond_path $pond_home/$pond_name
set -l pond_autoload_file $pond_path/{$pond_name}_{$pond_autoload_suffix}.fish

if ! test -f $pond_autoload_file
__pond_show_error "No autoload function in pond: $pond_name"; and return 1
end

cat $pond_autoload_file
end

function __pond_autounload_operation -a pond_name
set -l pond_path $pond_home/$pond_name
set -l pond_autounload_file $pond_path/{$pond_name}_{$pond_autounload_suffix}.fish
Expand All @@ -250,6 +267,17 @@ Usage:
$pond_editor $pond_autounload_file
end

function __pond_autounload_show_operation -a pond_name
set -l pond_path $pond_home/$pond_name
set -l pond_autounload_file $pond_path/{$pond_name}_{$pond_autounload_suffix}.fish

if ! test -f $pond_autounload_file
__pond_show_error "No autounload function in pond: $pond_name"; and return 1
end

cat $pond_autounload_file
end

function __pond_remove_operation -a pond_name
set -l pond_path $pond_home/$pond_name

Expand Down Expand Up @@ -831,41 +859,61 @@ Usage:
end
end
case autoload
set -l pond_name $argv[-1]
set argv $argv[1..-2]

if test -z "$pond_name"; or ! __pond_name_is_valid "$pond_name"
if ! argparse s/show >/dev/null 2>&1 -- $argv
__pond_autoload_command_usage
__pond_cleanup; and return 1
end

if test (count $argv) -eq 0
__pond_autoload_command_usage; and __pond_cleanup; and return 1
end

set -l pond_name $argv[-1]
set argv $argv[1..-2]

if test -z "$pond_name"; or ! __pond_name_is_valid "$pond_name"; or test (count $argv) -ne 0
__pond_autoload_command_usage; and __pond_cleanup; and return 1
else if ! __pond_exists $pond_name
__pond_show_not_exists_error $pond_name; and __pond_cleanup; and return 1
end

__pond_autoload_operation $pond_name
set -l exit_code $status
__pond_cleanup; and return $exit_code
if set -q _flag_show
__pond_autoload_show_operation $pond_name
set -l exit_code $status
__pond_cleanup; and return $exit_code
else
__pond_autoload_operation $pond_name
set -l exit_code $status
__pond_cleanup; and return $exit_code
end
case autounload
set -l pond_name $argv[-1]
set argv $argv[1..-2]

if test -z "$pond_name"; or ! __pond_name_is_valid "$pond_name"
if ! argparse s/show >/dev/null 2>&1 -- $argv
__pond_autounload_command_usage
__pond_cleanup; and return 1
end

if test (count $argv) -eq 0
__pond_autounload_command_usage; and __pond_cleanup; and return 1
end

set -l pond_name $argv[-1]
set argv $argv[1..-2]

if test -z "$pond_name"; or ! __pond_name_is_valid "$pond_name"; or test (count $argv) -ne 0
__pond_autounload_command_usage; and __pond_cleanup; and return 1
else if ! __pond_exists $pond_name
__pond_show_not_exists_error $pond_name; and __pond_cleanup; and return 1
end

__pond_autounload_operation $pond_name
set -l exit_code $status
__pond_cleanup; and return $exit_code
if set -q _flag_show
__pond_autounload_show_operation $pond_name
set -l exit_code $status
__pond_cleanup; and return $exit_code
else
__pond_autounload_operation $pond_name
set -l exit_code $status
__pond_cleanup; and return $exit_code
end
case dir
set -l pond_name $argv[-1]
set argv $argv[1..-2]
Expand Down
47 changes: 46 additions & 1 deletion tests/autoload.fish
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ source $parent/fixtures/variables.fish

set command_usage "\
Usage:
pond autoload <name>
pond autoload [options] <name>
Options:
-s, --show Show autoload function without opening editor
Arguments:
name The name of the pond for which an autoload function
will be opened in an editor and optionally created if
it does not already exist" >&2

set success_output_single_pond "\
function test-pond-1_autoload
end"

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

@echo "pond autoload $pond_name: success tests for pond without autoload function"
Expand Down Expand Up @@ -43,12 +51,49 @@ __pond_tear_down

__pond_editor_reset

for command in "pond autoload "{-s,--show}

@echo "$command: success tests for single pond"

__pond_setup 1 enabled loaded populated
@test "setup: autoload function exists" -f "$pond_home/$pond_name/"{$pond_name}_{$pond_autoload_suffix}.fish
@test "pond autoload: success exit code" (eval $command $pond_name >/dev/null 2>&1) $status -eq $success
@test "pond autoload: autoload function exists" -f "$pond_home/$pond_name/"{$pond_name}_{$pond_autoload_suffix}.fish
__pond_tear_down

@echo "$command: output tests for single pond"
__pond_setup 1 enabled loaded populated
@test "pond autoload: success output message" (eval $command $pond_name 2>&1 | string collect) = $success_output_single_pond
__pond_tear_down

end

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

for valid_option in -s --show
@test "pond autoload: fails for valid option $valid_option and missing pond name" (pond autoload $valid_option >/dev/null 2>&1) $status -eq $failure
@test "pond autoload: fails for valid option $valid_option and invalid pond name" (pond autoload $valid_option _invalid >/dev/null 2>&1) $status -eq $failure
end

for invalid_option in -i --invalid
@test "pond autoload: fails for invalid option $invalid_option and valid pond name" (pond autoload $invalid_option $pond_name >/dev/null 2>&1) $status -eq $failure
@test "pond autoload: fails for invalid option $invalid_option and invalid pond name" (pond autoload $invalid_option _invalid >/dev/null 2>&1) $status -eq $failure
end

@echo "pond autoload: validation failure output tests"
@test "pond autoload: command usage shown for missing pond name" (pond autoload 2>&1 | string collect) = $command_usage
@test "pond autoload: command usage shown for malformed pond name" (pond autoload _invalid 2>&1 | string collect) = $command_usage
@test "pond autoload: command error shown for non-existent pond" (pond autoload no-exist 2>&1 | string collect) = $not_exists_error

for valid_option in -y --yes
@test "pond autoload: command usage shown for valid option $valid_option and missing pond name" (pond autoload $valid_option 2>&1 | string collect) = $command_usage
@test "pond autoload: command usage shown for valid option $valid_option and invalid pond name" (pond autoload $valid_option _invalid 2>&1 | string collect) = $command_usage
end

for invalid_option in -i --invalid
@test "pond autoload: command usage shown for invalid option $invalid_option and valid pond name" (pond autoload $invalid_option $pond_name 2>&1 | string collect) = $command_usage
@test "pond autoload: command usage shown for invalid option $invalid_option and invalid pond name" (pond autoload $invalid_option _invalid 2>&1 | string collect) = $command_usage
end
47 changes: 46 additions & 1 deletion tests/autounload.fish
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ source $parent/fixtures/variables.fish

set command_usage "\
Usage:
pond autounload <name>
pond autounload [options] <name>
Options:
-s, --show Show autounload function without opening editor
Arguments:
name The name of the pond for which an autounload function
will be opened in an editor and optionally created if
it does not already exist"

set success_output_single_pond "\
function test-pond-1_autounload
end"

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

@echo "pond autounload $pond_name: success tests for pond without autounload function"
Expand Down Expand Up @@ -43,12 +51,49 @@ __pond_tear_down

__pond_editor_reset

for command in "pond autounload "{-s,--show}

@echo "$command: success tests for single pond"

__pond_setup 1 enabled loaded populated
@test "setup: autounload function exists" -f "$pond_home/$pond_name/"{$pond_name}_{$pond_autounload_suffix}.fish
@test "pond autounload: success exit code" (eval $command $pond_name >/dev/null 2>&1) $status -eq $success
@test "pond autounload: autounload function exists" -f "$pond_home/$pond_name/"{$pond_name}_{$pond_autounload_suffix}.fish
__pond_tear_down

@echo "$command: output tests for single pond"
__pond_setup 1 enabled loaded populated
@test "pond autounload: success output message" (eval $command $pond_name 2>&1 | string collect) = $success_output_single_pond
__pond_tear_down

end

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

for valid_option in -s --show
@test "pond autounload: fails for valid option $valid_option and missing pond name" (pond autounload $valid_option >/dev/null 2>&1) $status -eq $failure
@test "pond autounload: fails for valid option $valid_option and invalid pond name" (pond autounload $valid_option _invalid >/dev/null 2>&1) $status -eq $failure
end

for invalid_option in -i --invalid
@test "pond autounload: fails for invalid option $invalid_option and valid pond name" (pond autounload $invalid_option $pond_name >/dev/null 2>&1) $status -eq $failure
@test "pond autounload: fails for invalid option $invalid_option and invalid pond name" (pond autounload $invalid_option _invalid >/dev/null 2>&1) $status -eq $failure
end

@echo "pond autounload: validation failure output tests"
@test "pond autounload: command usage shown for missing pond name" (pond autounload 2>&1 | string collect) = $command_usage
@test "pond autounload: command usage shown for malformed pond name" (pond autounload _invalid 2>&1 | string collect) = $command_usage
@test "pond autounload: command error shown for non-existent pond" (pond autounload no-exist 2>&1 | string collect) = $not_exists_error

for valid_option in -y --yes
@test "pond autounload: command usage shown for valid option $valid_option and missing pond name" (pond autounload $valid_option 2>&1 | string collect) = $command_usage
@test "pond autounload: command usage shown for valid option $valid_option and invalid pond name" (pond autounload $valid_option _invalid 2>&1 | string collect) = $command_usage
end

for invalid_option in -i --invalid
@test "pond autounload: command usage shown for invalid option $invalid_option and valid pond name" (pond autounload $invalid_option $pond_name 2>&1 | string collect) = $command_usage
@test "pond autounload: command usage shown for invalid option $invalid_option and invalid pond name" (pond autounload $invalid_option _invalid 2>&1 | string collect) = $command_usage
end

0 comments on commit 92c1692

Please sign in to comment.