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

var eq empty string #45

Merged
merged 2 commits into from
Jan 27, 2025
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions bin/test-everything
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

set -eux -o pipefail

go test -v ./...
go build
bats test
49 changes: 49 additions & 0 deletions test/var.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
}
4 changes: 2 additions & 2 deletions var.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package main

import (
"errors"
"fmt"
"os"

"github.com/oalders/is/types"
Expand All @@ -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)
}
Expand Down
Loading