Skip to content

Commit

Permalink
feat: add support for Cangjie 0.57.3
Browse files Browse the repository at this point in the history
  • Loading branch information
gtn1024 committed Jan 10, 2025
1 parent 6618cc5 commit b56f604
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cjpm.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[dependencies]

[package]
cjc-version = "0.55.3"
cjc-version = "0.57.3"
compile-option = ""
description = "A Cangjie library to load environment variables from `.env`."
link-option = ""
Expand Down
11 changes: 6 additions & 5 deletions src/cjdotenv.cj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cjdotenv

import std.fs.File
import std.collection.HashMap
import std.os.{envVars, setEnv}
import std.process.{Process, CurrentProcess}
import std.fs.FSException

func filenamesOrDefault(filenames: Array<String>) {
Expand All @@ -20,25 +20,26 @@ func filenamesOrDefault(filenames: Array<String>) {
public func load(filenames: Array<String>) {
let newFilenames = filenamesOrDefault(filenames)

let process = Process.current
for (filename in newFilenames) {
loadFile(filename, false)
loadFile(filename, false, process)
}
}

func loadFile(filename: String, overload: Bool) {
func loadFile(filename: String, overload: Bool, process: CurrentProcess) {
let s = getFileData(filename)
let envMap = parse(s)

let currentEnv = HashMap<String, Bool>()
let rawEnv = envVars() /*cjlint-ignore !G.CHK.01 */
let rawEnv = process.environment

for ((k, v) in rawEnv) {
currentEnv[k] = true
}

for ((k, v) in envMap) {
if (!currentEnv.contains(k) || overload) {
setEnv(k, v)
process.setEnv(k, v)
}
}
}
Expand Down
38 changes: 18 additions & 20 deletions src/cjdotenv_test.cj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cjdotenv

import std.unittest.*
import std.unittest.testmacro.*
import std.os.*
import std.process.*
import std.collection.*

@When[os == "Windows"]
Expand All @@ -12,11 +12,10 @@ let emptyString = None<String>
let emptyString = ""

func clearenv() {
for (key in envVars().keys()) {
removeEnv(key)
let process = Process.current
for (key in process.environment.keys()) {
process.removeEnv(key)
}

@Expect(envVars().size, 0)
}

@Test
Expand All @@ -31,15 +30,15 @@ func testReadmeExample() {
let filename = "fixtures/readme.env"
load(filename)

let process = Process.current
let expectedValues = HashMap<String, ?String>()
expectedValues["DB_HOST"] = "localhost"
expectedValues["DB_USER"] = "postgres"
expectedValues["DB_PASS"] = "123456"
expectedValues["DB_DATABASE"] = "postgres"

@Expect(envVars().size, 4)
for ((k, v) in expectedValues) {
@Expect(getEnv(k), v)
@Expect(process.getEnv(k), v)
}
}

Expand All @@ -49,14 +48,14 @@ func testComments() {
let filename = "fixtures/comments.env"
load(filename)

let process = Process.current
let expectedValues = HashMap<String, ?String>()
expectedValues["foo"] = "bar"
expectedValues["bar"] = "foo#baz"
expectedValues["baz"] = "foo"

@Expect(envVars().size, 3)
for ((k, v) in expectedValues) {
@Expect(getEnv(k), v)
@Expect(process.getEnv(k), v)
}
}

Expand All @@ -66,12 +65,12 @@ func testEquals() {
let filename = "fixtures/equals.env"
load(filename)

let process = Process.current
let expectedValues = HashMap<String, ?String>()
expectedValues["OPTION_A"] = "postgres://localhost:5432/database?sslmode=disable"

@Expect(envVars().size, 1)
for ((k, v) in expectedValues) {
@Expect(getEnv(k), v)
@Expect(process.getEnv(k), v)
}
}

Expand All @@ -81,13 +80,13 @@ func testExported() {
let filename = "fixtures/exported.env"
load(filename)

let process = Process.current
let expectedValues = HashMap<String, ?String>()
expectedValues["OPTION_A"] = "2"
expectedValues["OPTION_B"] = "\\n"

@Expect(envVars().size, 2)
for ((k, v) in expectedValues) {
@Expect(getEnv(k), v)
@Expect(process.getEnv(k), v)
}
}

Expand All @@ -101,10 +100,10 @@ func testInvalid() {
@Test
func testPlain() {
clearenv()

let filename = "fixtures/plain.env"
load(filename)

let process = Process.current
let expectedValues = HashMap<String, ?String>()
expectedValues["OPTION_A"] = "1"
expectedValues["OPTION_B"] = "2"
Expand All @@ -122,9 +121,8 @@ func testPlain() {
}
}

@Expect(envVars().size, expected)
for ((k, v) in expectedValues) {
@Expect(getEnv(k), v)
@Expect(process.getEnv(k), v)
}
}

Expand All @@ -134,6 +132,7 @@ func testQuoted() {
let filename = "fixtures/quoted.env"
load(filename)

let process = Process.current
let expectedValues = HashMap<String, ?String>()
expectedValues["OPTION_A"] = "1"
expectedValues["OPTION_B"] = "2"
Expand All @@ -156,9 +155,8 @@ func testQuoted() {
}
}

@Expect(envVars().size, expected)
for ((k, v) in expectedValues) {
@Expect(getEnv(k), v)
@Expect(process.getEnv(k), v)
}
}

Expand All @@ -168,6 +166,7 @@ func testSubstitutions() {
let filename = "fixtures/substitutions.env"
load(filename)

let process = Process.current
let expectedValues = HashMap<String, ?String>()
expectedValues["OPTION_A"] = "1"
expectedValues["OPTION_B"] = "1"
Expand All @@ -191,8 +190,7 @@ func testSubstitutions() {
}
}

@Expect(envVars().size, expected)
for ((k, v) in expectedValues) {
@Expect(getEnv(k), v)
@Expect(process.getEnv(k), v)
}
}

0 comments on commit b56f604

Please sign in to comment.