Skip to content

Commit

Permalink
feat(*): add environment variable and random functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Nov 11, 2023
1 parent 1e6d859 commit af9acbe
Show file tree
Hide file tree
Showing 11 changed files with 445 additions and 45 deletions.
29 changes: 19 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ The scope of what is covered by the version number excludes:

- create a release branch
- update the changelog below
- update version and copyright-years in `./LICENSE.md` and `./src/[module-name]/init.lua` (in doc-comments
header, and in module constants)
- update version and copyright-years in `./LICENSE.md` and `./src/time.c` (in module constants)
- create a new rockspec and update the version inside the new rockspec:<br/>
`cp [module-name]-scm-1.rockspec ./rockspecs/[module-name]-X.Y.Z-1.rockspec`
- test: run `make test` and `make lint`
- clean and render the docs: run `make clean` and `make docs`
- commit the changes as `release X.Y.Z`
`cp luasystem-scm-0.rockspec ./rockspecs/luasystem-X.Y.Z-1.rockspec`
- clean and render the docs: run `ldoc .`
- commit the changes as `Release vX.Y.Z`
- push the commit, and create a release PR
- after merging tag the release commit with `X.Y.Z`
- after merging tag the release commit with `vX.Y.Z`
- upload to LuaRocks:<br/>
`luarocks upload ./rockspecs/[module-name]-X.Y.Z-1.rockspec --api-key=ABCDEFGH`
- test the newly created rock:<br/>
Expand All @@ -32,9 +30,20 @@ The scope of what is covered by the version number excludes:

### Version X.Y.Z, unreleased

- a fix
- a change
- Change: `gettime` is deprecated. Use `unixtime` instead.
- Feat: `unixtime` returns the time since the Unix epoch.
- Feat: `windowstime` returns the time since the Windows epoch.
- Feat: `time` returns the system time.
- Feat: `setenv` added to set environment variables.
- Feat: `getenvs` added to list environment variables.
- Feat: `random` added to return high-quality random bytes

### Version 0.1.0, released 01-Jan-2022
### Version 0.2.1, released 02-Oct-2016

### Version 0.2.0, released 08-May-2016

### Version 0.1.1, released 10-Apr-2016

### Version 0.1.0, released 11-Feb-2016

- initial release
4 changes: 2 additions & 2 deletions luasystem-scm-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ version = package_version.."-"..rockspec_revision
source = {
url = "git+https://github.com/"..github_account_name.."/"..github_repo_name..".git",
branch = (package_version == "scm") and "master" or nil,
tag = (package_version ~= "scm") and package_version or nil,
tag = (package_version ~= "scm") and "v"..package_version or nil,
}

description = {
Expand Down Expand Up @@ -45,7 +45,7 @@ local function make_platform(plat)
return {
modules = {
['system.core'] = {
sources = { 'src/core.c', 'src/compat.c', 'src/time.c', },
sources = { 'src/core.c', 'src/compat.c', 'src/time.c', 'src/environment.c', 'src/random.c' },
defines = defines[plat],
libraries = libraries[plat],
},
Expand Down
2 changes: 1 addition & 1 deletion rockspecs/luasystem-0.2.1-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ version = package_version.."-"..rockspec_revision
source = {
url = "git+https://github.com/"..github_account_name.."/"..github_repo_name..".git",
branch = (package_version == "scm") and "master" or nil,
tag = (package_version ~= "scm") and package_version or nil,
tag = (package_version ~= "scm") and "v"..package_version or nil,
}

description = {
Expand Down
77 changes: 77 additions & 0 deletions spec/01-time_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
local system = require 'system.core'

describe('Test time functions', function()

-- returns the new second, on the new second
local function wait_for_second_rollover()
local start_time = math.floor(os.time())
local end_time = math.floor(os.time())
while end_time == start_time do
end_time = math.floor(os.time())
end
return end_time
end


describe("time()", function()

it('returns current time', function()
wait_for_second_rollover()
local lua_time = wait_for_second_rollover()
local aaa_time = system.time()
assert.is.near(lua_time, aaa_time, 0.01)

wait_for_second_rollover()
assert.is.near(1, system.time() - aaa_time, 0.01)
end)

end)



describe("monotime()", function()

it('returns monotonically increasing time', function()
local starttime = system.monotime()
local endtime = system.monotime()
local delta = endtime - starttime
assert.is_true(starttime > 0)
assert.is_true(delta >= 0)
assert.is_true(system.monotime() - endtime >= 0)
end)

end)



describe("sleep()", function()

it("should sleep for the specified time", function()
local start_time = wait_for_second_rollover()
system.sleep(1)
local end_time = os.time()
local elapsed_time = end_time - start_time
assert.is.near(elapsed_time, 1, 0.01)
end)


it("should sleep for the specified time; fractional", function()
local start_time = system.time()
system.sleep(0.5)
local end_time = system.time()
local elapsed_time = end_time - start_time
assert.is.near(0.5, elapsed_time, 0.01)
end)


it("should return immediately for a non-positive sleep time", function()
local start_time = wait_for_second_rollover()
system.sleep(-1)
local end_time = os.time()
local elapsed_time = end_time - start_time
assert.is.near(elapsed_time, 0, 0.001)
end)

end)

end)
47 changes: 47 additions & 0 deletions spec/02-random_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local system = require("system")

describe("Random:", function()

describe("random()", function()

it("should return random bytes for a valid number of bytes", function()
local num_bytes = system.MAX_RANDOM_BUFFER_SIZE
local result, err_msg = system.random(num_bytes)
assert.is_nil(err_msg)
assert.is.string(result)
assert.is_equal(num_bytes, #result)
end)


it("should return an error message for an invalid number of bytes", function()
local num_bytes = 0
local result, err_msg = system.random(num_bytes)
assert.is.falsy(result)
assert.are.equal("invalid number of bytes, must be between 1 and 1024", err_msg)
end)


it("should return an error message for exceeding the maximum buffer size", function()
local num_bytes = system.MAX_RANDOM_BUFFER_SIZE + 1
local result, err_msg = system.random(num_bytes)
assert.is.falsy(result)
assert.are.equal("invalid number of bytes, must be between 1 and 1024", err_msg)
end)


it("should not return duplicate results", function()
local num_bytes = 10
local result1, err_msg = system.random(num_bytes)
assert.is_nil(err_msg)
assert.is.string(result1)

local result2, err_msg = system.random(num_bytes)
assert.is_nil(err_msg)
assert.is.string(result2)

assert.is_not.equal(result1, result2)
end)

end)

end)
76 changes: 76 additions & 0 deletions spec/03-environment_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
-- Import the library that contains the environment-related functions
local system = require("system")

describe("Environment Variables:", function()

describe("setenv()", function()

it("should set an environment variable", function()
assert.is_true(system.setenv("TEST_VAR", "test_value"))
assert.is_equal("test_value", os.getenv("TEST_VAR"))
end)


it("should set an empty environment variable value", function()
assert.is_true(system.setenv("TEST_VAR", ""))
assert.is_equal("", os.getenv("TEST_VAR"))
end)


it("should set an empty environment variable", function()
assert.is_true(system.setenv("TEST_VAR", ""))
assert.is_equal("", os.getenv("TEST_VAR"))
end)


it("should error on input bad type", function()
assert.has_error(function()
system.setenv("TEST_VAR", {})
end)
assert.has_error(function()
system.setenv({}, "test_value")
end)
end)


it("should return success on deleting a variable that doesn't exist", function()
if os.getenv("TEST_VAR") ~= nil then
-- clear if it was already set
assert.is_true(system.setenv("TEST_VAR", nil))
end

assert.is_true(system.setenv("TEST_VAR", nil)) -- clear again shouldn't fail
end)

end)



describe("getenvs()", function()

it("should list environment variables", function()
assert.is_true(system.setenv("TEST_VAR1", nil))
assert.is_true(system.setenv("TEST_VAR2", nil))
assert.is_true(system.setenv("TEST_VAR3", nil))
local envVars1 = system.getenvs()
assert.is_true(system.setenv("TEST_VAR1", "test_value1"))
assert.is_true(system.setenv("TEST_VAR2", "test_value2"))
assert.is_true(system.setenv("TEST_VAR3", "test_value3"))
local envVars2 = system.getenvs()
assert.is_true(system.setenv("TEST_VAR1", nil))
assert.is_true(system.setenv("TEST_VAR2", nil))
assert.is_true(system.setenv("TEST_VAR3", nil))

for k,v in pairs(envVars1) do
envVars2[k] = nil
end
assert.are.same({
TEST_VAR1 = "test_value1",
TEST_VAR2 = "test_value2",
TEST_VAR3 = "test_value3",
}, envVars2)
end)

end)

end)
31 changes: 0 additions & 31 deletions spec/time_spec.lua

This file was deleted.

4 changes: 4 additions & 0 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#endif

void time_open(lua_State *L);
void environment_open(lua_State *L);
void random_open(lua_State *L);

/*-------------------------------------------------------------------------
* Initializes all library modules.
Expand All @@ -23,5 +25,7 @@ LUAEXPORT int luaopen_system_core(lua_State *L) {
lua_pushstring(L, LUASYSTEM_VERSION);
lua_rawset(L, -3);
time_open(L);
random_open(L);
environment_open(L);
return 1;
}
Loading

0 comments on commit af9acbe

Please sign in to comment.