From cf19735324e2473d60dd1555293605c9079eb84c Mon Sep 17 00:00:00 2001 From: Olaf Alders Date: Mon, 27 Jan 2025 15:32:46 -0500 Subject: [PATCH 1/2] Add bin/test-everything --- bin/test-everything | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100755 bin/test-everything diff --git a/bin/test-everything b/bin/test-everything new file mode 100755 index 0000000..003a04f --- /dev/null +++ b/bin/test-everything @@ -0,0 +1,7 @@ +#!/bin/bash + +set -eux -o pipefail + +go test -v ./... +go build +bats test From 7fffda9847b238beb4f670e8a77ab7cdc60d6b79 Mon Sep 17 00:00:00 2001 From: Olaf Alders Date: Mon, 27 Jan 2025 15:11:04 -0500 Subject: [PATCH 2/2] Allow for var comparisons with empty strings --- CHANGELOG.md | 4 ++++ README.md | 27 +++++++++++++++++++++++++++ api.go | 8 +++++++- test/var.bats | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ var.go | 4 ++-- 5 files changed, 89 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61f2bb0..6d06661 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changes +- Allow for empty string comparisons via "eq" and "ne" in "is var". e.g. + - is var FOO eq "" + - is var FOO ne "" + ## 0.6.0 - 2025-01-20 - Add var subcommand diff --git a/README.md b/README.md index a9f2ed5..9be73a4 100644 --- a/README.md +++ b/README.md @@ -767,6 +767,19 @@ Check if environment variables are set, unset or contain a specific value. ```bash is var EDITOR set && is var EDITOR like vim + +is var EDITOR set && is var EDITOR eq "" +``` + +`is var` can only operate on environment variables which are available in its +environment. This means that you may need to export some variables before you +can invoke `is var`. For example, if you have created a variable in the current +script, it should be exported before `is var` is used. + +```shell +SLOW_HORSE=rivercartwright +export SLOW_HORSE +is var SLOW_HORSE like cart && echo "blue shirt, white tee" ``` #### set @@ -796,6 +809,20 @@ Supported comparisons are: - `like` - `unlike` +Both `eq` and `ne` allow for comparisons with an empty string: + +```shell +SET_BUT_EMPTY= +export SET_BUT_EMPTY + +is var SET_BUT_EMPTY set && is var SET_BUT_EMPTY eq "" + +NOT_EMPTY=lebowski +export NOT_EMPTY + +is var NOT_EMPTY set && is var NOT_EMPTY ne "" +``` + ##### --compare Optional argument to command. Defaults to `optimistic`. Because comparisons diff --git a/api.go b/api.go index 69a18c2..d93ecc4 100644 --- a/api.go +++ b/api.go @@ -82,8 +82,14 @@ type VarCmd struct { Compare string `default:"optimistic" enum:"float,integer,string,version,optimistic" help:"[float|integer|string|version|optimistic]"` } +// Allow for the following: +// +// is var EDITOR eq "" +// is var EDITOR ne "" +// +// . func (r *VarCmd) Validate() error { - if r.Op != "set" && r.Op != "unset" && r.Val == "" { + if r.Op != "set" && r.Op != "unset" && r.Op != "eq" && r.Op != "ne" && r.Val == "" { return fmt.Errorf("missing required argument: val") } return nil diff --git a/test/var.bats b/test/var.bats index f138473..2ae0ba2 100755 --- a/test/var.bats +++ b/test/var.bats @@ -94,3 +94,52 @@ teardown() { [ "$status" -eq 1 ] [[ "$output" == *"wanted result must be an integer"* ]] } + +@test "is var EMPTY_VAR eq '' should pass" { + export EMPTY_VAR="" + run ./is var EMPTY_VAR eq "" + [ "$status" -eq 0 ] +} + +# we can't determine the difference between eq "" and eq with no trailing arg +@test "'is var EMPTY_VAR eq' should pass" { + export EMPTY_VAR="" + run ./is var EMPTY_VAR eq + [ "$status" -eq 0 ] +} + +@test "is var EMPTY_VAR eq \"\" should fail" { + run ./is var EMPTY_VAR eq "" + [ "$status" -eq 1 ] +} + +@test "is var EMPTY_VAR set should pass" { + export EMPTY_VAR="" + run ./is var EMPTY_VAR set + [ "$status" -eq 0 ] +} + +@test "is var EMPTY_VAR unset should fail" { + export EMPTY_VAR="" + run ./is var EMPTY_VAR unset + [ "$status" -eq 1 ] +} + +@test "is var EMPTY_VAR ne '' should fail" { + export EMPTY_VAR="" + run ./is var EMPTY_VAR ne "" + [ "$status" -eq 1 ] +} + +# we can't determine the difference between ne "" and ne with no trailing arg +@test "is var EMPTY_VAR ne should fail" { + export EMPTY_VAR="" + run ./is var EMPTY_VAR ne + [ "$status" -eq 1 ] +} + +@test "is var EMPTY_VAR ne 'non-empty' should pass" { + export EMPTY_VAR="" + run ./is var EMPTY_VAR ne "non-empty" + [ "$status" -eq 0 ] +} diff --git a/var.go b/var.go index bc89d1f..a12aedb 100644 --- a/var.go +++ b/var.go @@ -2,7 +2,7 @@ package main import ( - "errors" + "fmt" "os" "github.com/oalders/is/types" @@ -27,7 +27,7 @@ func (r *VarCmd) Run(ctx *types.Context) error { default: val, exists := os.LookupEnv(r.Name) if !exists { - return errors.New("environment variable not set") + return fmt.Errorf("environment variable %s is not set", r.Name) } return compareOutput(ctx, r.Compare, r.Op, val, r.Val) }