|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { BashEnv } from "../../BashEnv.js"; |
| 3 | + |
| 4 | +describe("export builtin", () => { |
| 5 | + describe("setting variables", () => { |
| 6 | + it("should set a variable with export NAME=value", async () => { |
| 7 | + const env = new BashEnv(); |
| 8 | + await env.exec("export FOO=bar"); |
| 9 | + const result = await env.exec("echo $FOO"); |
| 10 | + expect(result.stdout).toBe("bar\n"); |
| 11 | + }); |
| 12 | + |
| 13 | + it("should set multiple variables", async () => { |
| 14 | + const env = new BashEnv(); |
| 15 | + await env.exec("export FOO=bar BAZ=qux"); |
| 16 | + const result = await env.exec("echo $FOO $BAZ"); |
| 17 | + expect(result.stdout).toBe("bar qux\n"); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should handle value with equals sign", async () => { |
| 21 | + const env = new BashEnv(); |
| 22 | + await env.exec("export URL=http://example.com?foo=bar"); |
| 23 | + const result = await env.exec("echo $URL"); |
| 24 | + expect(result.stdout).toBe("http://example.com?foo=bar\n"); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should create empty variable when NAME has no value", async () => { |
| 28 | + const env = new BashEnv(); |
| 29 | + await env.exec("export EMPTY"); |
| 30 | + const result = await env.exec('test -z "$EMPTY" && echo empty'); |
| 31 | + expect(result.stdout).toBe("empty\n"); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should preserve existing variable value with export NAME", async () => { |
| 35 | + const env = new BashEnv({ env: { EXISTING: "value" } }); |
| 36 | + await env.exec("export EXISTING"); |
| 37 | + const result = await env.exec("echo $EXISTING"); |
| 38 | + expect(result.stdout).toBe("value\n"); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe("listing variables", () => { |
| 43 | + it("should list all exported variables with no args", async () => { |
| 44 | + const env = new BashEnv({ env: { FOO: "bar", BAZ: "qux" } }); |
| 45 | + const result = await env.exec("export"); |
| 46 | + expect(result.stdout).toContain("declare -x FOO='bar'"); |
| 47 | + expect(result.stdout).toContain("declare -x BAZ='qux'"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should list all exported variables with -p", async () => { |
| 51 | + const env = new BashEnv({ env: { FOO: "bar" } }); |
| 52 | + const result = await env.exec("export -p"); |
| 53 | + expect(result.stdout).toContain("declare -x FOO='bar'"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should escape single quotes in values", async () => { |
| 57 | + const env = new BashEnv(); |
| 58 | + await env.exec("export MSG=\"it's working\""); |
| 59 | + const result = await env.exec("export"); |
| 60 | + expect(result.stdout).toContain("it'\\''s working"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should not list aliases", async () => { |
| 64 | + const env = new BashEnv(); |
| 65 | + await env.exec("alias ll='ls -la'"); |
| 66 | + await env.exec("export FOO=bar"); |
| 67 | + const result = await env.exec("export"); |
| 68 | + expect(result.stdout).not.toContain("BASH_ALIAS"); |
| 69 | + expect(result.stdout).toContain("FOO"); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe("un-exporting with -n", () => { |
| 74 | + it("should remove variable with -n", async () => { |
| 75 | + const env = new BashEnv({ env: { FOO: "bar" } }); |
| 76 | + await env.exec("export -n FOO"); |
| 77 | + const result = await env.exec('test -z "$FOO" && echo removed'); |
| 78 | + expect(result.stdout).toBe("removed\n"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should remove multiple variables with -n", async () => { |
| 82 | + const env = new BashEnv({ env: { FOO: "bar", BAZ: "qux" } }); |
| 83 | + await env.exec("export -n FOO BAZ"); |
| 84 | + const result = await env.exec( |
| 85 | + 'test -z "$FOO" && test -z "$BAZ" && echo removed', |
| 86 | + ); |
| 87 | + expect(result.stdout).toBe("removed\n"); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe("variable usage", () => { |
| 92 | + it("exported variable should be available in command", async () => { |
| 93 | + const env = new BashEnv(); |
| 94 | + await env.exec("export GREETING=hello"); |
| 95 | + const result = await env.exec("echo $GREETING world"); |
| 96 | + expect(result.stdout).toBe("hello world\n"); |
| 97 | + }); |
| 98 | + |
| 99 | + it("exported variable should be available in subshell", async () => { |
| 100 | + const env = new BashEnv(); |
| 101 | + await env.exec("export FOO=bar"); |
| 102 | + const result = await env.exec("(echo $FOO)"); |
| 103 | + expect(result.stdout).toBe("bar\n"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should work with conditional", async () => { |
| 107 | + const env = new BashEnv(); |
| 108 | + await env.exec("export DEBUG=1"); |
| 109 | + const result = await env.exec('[ "$DEBUG" = "1" ] && echo debug_on'); |
| 110 | + expect(result.stdout).toBe("debug_on\n"); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments