-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env bats | ||
|
||
bats_require_minimum_version 1.5.0 | ||
|
||
setup() { | ||
export TEST_VAR="nvim" | ||
} | ||
|
||
teardown() { | ||
unset TEST_VAR | ||
} | ||
|
||
@test "is var TEST_VAR set" { | ||
run ./is var TEST_VAR set | ||
[ "$status" -eq 0 ] | ||
} | ||
|
||
@test "is var TEST_VAR unset" { | ||
run ./is var TEST_VAR unset | ||
[ "$status" -eq 1 ] | ||
} | ||
|
||
@test "is var TEST_VAR eq nvim" { | ||
run ./is var TEST_VAR eq nvim | ||
[ "$status" -eq 0 ] | ||
} | ||
|
||
@test "is var TEST_VAR eq vim" { | ||
run ./is var TEST_VAR eq vim | ||
[ "$status" -eq 1 ] | ||
} | ||
|
||
@test "is var TEST_VAR gt 1" { | ||
run ./is var TEST_VAR gt 1 | ||
[ "$status" -eq 1 ] | ||
} | ||
|
||
@test "is var TEST_VAR like nv.*" { | ||
run ./is var TEST_VAR like "nv.*" | ||
[ "$status" -eq 0 ] | ||
} | ||
|
||
@test "is var TEST_VAR unlike nv.*" { | ||
run ./is var TEST_VAR unlike "nv.*" | ||
[ "$status" -eq 1 ] | ||
} | ||
|
||
@test "is var NON_EXISTENT_VAR set" { | ||
run ./is var NON_EXISTENT_VAR set | ||
[ "$status" -eq 1 ] | ||
} | ||
|
||
@test "is var NON_EXISTENT_VAR unset" { | ||
run ./is var NON_EXISTENT_VAR unset | ||
[ "$status" -eq 0 ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// This file handles environment variable parsing | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/oalders/is/compare" | ||
"github.com/oalders/is/types" | ||
) | ||
|
||
func (r *VarCmd) Run(ctx *types.Context) error { | ||
ctx.Success = false | ||
|
||
switch r.Op { | ||
case "set": | ||
_, exists := os.LookupEnv(r.Name) | ||
if exists { | ||
ctx.Success = true | ||
} | ||
return nil | ||
case "unset": | ||
_, exists := os.LookupEnv(r.Name) | ||
if !exists { | ||
ctx.Success = true | ||
} | ||
return nil | ||
default: | ||
val, exists := os.LookupEnv(r.Name) | ||
if !exists { | ||
return errors.New("environment variable not set") | ||
} | ||
return compare.Optimistic(ctx, r.Op, val, r.Val) | ||
} | ||
} |