From 23da24ddb9306174e9a6ca00a6d31efe99484748 Mon Sep 17 00:00:00 2001 From: Pinta365 Date: Mon, 4 Mar 2024 20:25:46 +0100 Subject: [PATCH] deno tests --- README.md | 11 ++-- deno.jsonc | 4 +- mod.ts | 1 + tests/deno.ts | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+), 5 deletions(-) create mode 100644 tests/deno.ts diff --git a/README.md b/README.md index 495666e..38bce43 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ -## **Flexible Environment Variable Management for Deno, Bun, and Node.js** +## Cross-runtime environment variable management for Deno, Bun, and Node.js + +[![JSR Version](https://jsr.io/badges/@cross/env)](https://jsr.io/@cross/env) +[![JSR Score](https://jsr.io/badges/@cross/env/score)](https://jsr.io/@cross/env/score) This library provides a consistent and simple interface for managing environment variables across multiple runtimes, making it ideal for cross-platform development. -## **Features** +## Features - **Cross-runtime support:** Works seamlessly within Deno, Bun, and Node.js environments. - **Get and Set environment variables:** Retrieve and Modify environment variables in a consistent interface across @@ -12,7 +15,7 @@ making it ideal for cross-platform development. - **Error handling:** Provides clear error messages for unsupported runtimes or validation failures. - **Optional environmental file loading:** Supports loading variables from custom .env files _(experimental)_ -## **Installation** +## Installation ```bash #For Deno @@ -94,7 +97,7 @@ function isPositiveNumber(value: string): boolean { const timeout = validateAndGetEnv("TIMEOUT", isPositiveNumber); ``` -## **Configuration (optional):** +## Configuration (optional) For more advanced use cases you can configure the behaviour of the library. The library defaults to showing console warnings but not throwing errors. diff --git a/deno.jsonc b/deno.jsonc index eaff856..5e7af7a 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,7 +1,9 @@ { "tasks": { - "publish": "deno publish --config jsr.json" + "test": "deno test --allow-env ./tests/deno.ts", + "publish": "deno publish --config jsr.jsonc" }, + "lock": false, "fmt": { "lineWidth": 120, "indentWidth": 4 diff --git a/mod.ts b/mod.ts index 96716a4..e8a9d24 100644 --- a/mod.ts +++ b/mod.ts @@ -12,6 +12,7 @@ import { ValidatorFunction, } from "./lib/helpers.ts"; import { loadEnvFile } from "./lib/filehandler.ts"; +export { FileReadError, UnsupportedEnvironmentError, ValidationError } from "./lib/helpers.ts"; export type { ValidatorFunction } from "./lib/helpers.ts"; /** diff --git a/tests/deno.ts b/tests/deno.ts new file mode 100644 index 0000000..556d2a4 --- /dev/null +++ b/tests/deno.ts @@ -0,0 +1,160 @@ +import { assert, assertEquals, assertThrows } from "https://deno.land/std@0.218.2/assert/mod.ts"; + +import { + getAllEnv, + getEnv, + hasEnv, + setEnv, + setupEnv, + validateAndGetEnv, + validateEnv, + ValidationError, + ValidatorFunction, +} from "../mod.ts"; + +/** ==== getEnv() ==== */ +Deno.test({ + name: "getEnv() retrieves an existing environment variable", + fn() { + Deno.env.set("TEST_VARIABLE", "hello"); + + const value = getEnv("TEST_VARIABLE"); + assertEquals(value, "hello"); + + Deno.env.delete("TEST_VARIABLE"); + }, +}); + +Deno.test({ + name: "getEnv() undefined for nonexistant keys", + fn: () => { + const result = getEnv("TEST_KEY_NONEXISTANT"); + assertEquals(result, undefined); + }, +}); + +/** ==== getAllEnv() ==== */ +Deno.test({ + name: "getAllEnv() with prefix filters variables", + fn: () => { + Deno.env.set("API_KEY", "12345"); + Deno.env.set("API_VERSION", "v2"); + Deno.env.set("OTHER_VAR", "test"); + + const apiVariables = getAllEnv("API_"); + + assertEquals(apiVariables, { + API_KEY: "12345", + API_VERSION: "v2", + }); + + Deno.env.delete("API_KEY"); + Deno.env.delete("API_VERSION"); + Deno.env.delete("OTHER_VAR"); + }, +}); + +/** ==== setEnv() ==== */ +Deno.test({ + name: "setEnv() creates a new environment variable", + fn: () => { + const key = "TEST_VARIABLE"; + const value = "test_value"; + + setEnv(key, value); + assertEquals(Deno.env.get(key), value); + + Deno.env.delete(key); + }, +}); + +Deno.test({ + name: "setEnv() overwrites an existing environment variable", + fn: () => { + const key = "TEST_VARIABLE"; + Deno.env.set(key, "old_value"); + + setEnv(key, "new_value"); + assertEquals(Deno.env.get(key), "new_value"); + + Deno.env.delete(key); + }, +}); + +/** ==== hasEnv() ==== */ +Deno.test({ + name: "hasEnv() returns true for an existing environment variable", + fn: () => { + const key = "TEST_VARIABLE"; + Deno.env.set(key, "value"); + + assert(hasEnv(key)); + + Deno.env.delete(key); + }, +}); + +Deno.test({ + name: "hasEnv() returns false for a non-existent environment variable", + fn: () => { + const key = "NON_EXISTENT_VARIABLE"; + assertEquals(hasEnv(key), false); + }, +}); + +/** ==== validateEnv() ==== */ +const isValidPort: ValidatorFunction = (value: string): boolean => /^\d+$/.test(value); + +Deno.test({ + name: "validateEnv() returns true for valid values", + fn: () => { + Deno.env.set("TEST_VALUE", "1234"); // Valid as per isValidPort + assert(validateEnv("TEST_VALUE", isValidPort)); + Deno.env.delete("TEST_VALUE"); + }, +}); + +Deno.test({ + name: "validateEnv() false for nonexistant keys", + fn: () => { + const result = validateEnv("TEST_VALUE_NONEXISTANT", isValidPort); + assertEquals(result, false); + }, +}); + +/** ==== validateAndGetEnv() ==== */ +Deno.test({ + name: "validateAndGetEnv() returns the value for valid values", + fn: () => { + Deno.env.set("TEST_PORT", "8080"); + const port = validateAndGetEnv("TEST_PORT", isValidPort); + assertEquals(port, "8080"); + Deno.env.delete("TEST_PORT"); + }, +}); + +Deno.test({ + name: "validateAndGetEnv() returns undefined for invalid values", + fn: () => { + Deno.env.set("PORT", "http"); // Invalid as per isValidPort + + // should return undefined + const result = validateAndGetEnv("PORT", isValidPort); + assertEquals(result, undefined); + + Deno.env.delete("PORT"); + }, +}); + +Deno.test({ + name: "validateAndGetEnv() throws for invalid values", + fn: () => { + Deno.env.set("PORT", "http"); // Invalid as per isValidPort + + setupEnv({ throwErrors: true }); + //it should throw an error: + assertThrows(() => validateAndGetEnv("PORT", isValidPort), ValidationError); + + Deno.env.delete("PORT"); + }, +});