Skip to content

Commit

Permalink
fix(tmp): use stdpath('cache') for tmp files (mistweaverco#284)
Browse files Browse the repository at this point in the history
* fix(tmp): use `stdpath('cache')` for tmp files

This should fix: mistweaverco#282

* Add windows testrunner dockerfile

* fix(ci): workflow file

* refactor(ci): add optional windows test pipeline

* feat(ci): windows

* fix(fs): join_paths on windows

* fix(ci): windows cache

* fix(fs): gsub returns two params

* debug(ci): windows caching

* feat(tests): windows fs.join_paths

* refactor(ci): tests
  • Loading branch information
gorillamoe authored and iamxiaojianzheng committed Oct 24, 2024
1 parent 69262a8 commit e1d6404
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 66 deletions.
43 changes: 0 additions & 43 deletions .github/workflows/conform-code.yml

This file was deleted.

68 changes: 68 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
name: Tests

on:
pull_request:
paths:
- '**/*.lua'
branches:
- main

jobs:
test-linux:
name: Test Code on Linux
runs-on: ubuntu-latest
container:
image: ghcr.io/mistweaverco/kulala-nvim-testrunner:latest
steps:
- uses: actions/checkout@v4
- name: Restore cache
uses: actions/cache@v4
with:
path: |
.tests
key: ${{ runner.os }}-tests
- name: Run tests
run: ./scripts/tests.sh run
lint:
name: Lint Code
runs-on: ubuntu-latest
container:
image: ghcr.io/mistweaverco/kulala-nvim-testrunner:latest
steps:
- uses: actions/checkout@v4

- name: Run linter
run: ./scripts/lint.sh check-code
test-windows:
name: Test Code on Windows
runs-on: windows-latest
if: ${{ github.event.label.name == 'needs-windows-test' }}
steps:
- uses: actions/checkout@v4
- name: Restore cache
id: cache-deps
uses: actions/cache@v4
with:
path: |
.tests
~\scoop
~\AppData\Roaming\LJ4W
~\AppData\Roaming\luarocks
key: ${{ runner.os }}-tests
- name: Check if cache hit
if: steps.cache-deps.outputs.cache-hit == 'true'
run: echo "GH_CACHE_HIT=true" >> $Env:GITHUB_ENV
- name: Install dependencies
run: ./scripts/install-ci-test-requirements.ps1
- name: Save cache
uses: actions/cache/save@v4
with:
path: |
.tests
~\scoop
~\AppData\Roaming\LJ4W
~\AppData\Roaming\luarocks
key: ${{ runner.os }}-tests
- name: Run tests
run: ./scripts/tests.ps1
16 changes: 8 additions & 8 deletions lua/kulala/parser/scripts/engines/javascript/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ local NPM_EXISTS = vim.fn.executable("npm") == 1
local NODE_EXISTS = vim.fn.executable("node") == 1
local SCRIPTS_DIR = FS.get_scripts_dir()
local REQUEST_SCRIPTS_DIR = FS.get_request_scripts_dir()
local SCRIPTS_BUILD_DIR = FS.get_tmp_scripts_build_dir()
local BASE_DIR = FS.join_paths(SCRIPTS_DIR, "engines", "javascript", "lib")
local BASE_FILE_PRE_CLIENT_ONLY =
FS.join_paths(SCRIPTS_DIR, "engines", "javascript", "lib", "dist", "pre_request_client_only.js")
local BASE_FILE_PRE = FS.join_paths(SCRIPTS_DIR, "engines", "javascript", "lib", "dist", "pre_request.js")
local BASE_FILE_POST_CLIENT_ONLY =
FS.join_paths(SCRIPTS_DIR, "engines", "javascript", "lib", "dist", "post_request_client_only.js")
local BASE_FILE_POST = FS.join_paths(SCRIPTS_DIR, "engines", "javascript", "lib", "dist", "post_request.js")
local BASE_FILE_PRE_CLIENT_ONLY = FS.join_paths(SCRIPTS_BUILD_DIR, "dist", "pre_request_client_only.js")
local BASE_FILE_PRE = FS.join_paths(SCRIPTS_BUILD_DIR, "dist", "pre_request.js")
local BASE_FILE_POST_CLIENT_ONLY = FS.join_paths(SCRIPTS_BUILD_DIR, "dist", "post_request_client_only.js")
local BASE_FILE_POST = FS.join_paths(SCRIPTS_BUILD_DIR, "dist", "post_request.js")
local FILE_MAPPING = {
pre_request_client_only = BASE_FILE_PRE_CLIENT_ONLY,
pre_request = BASE_FILE_PRE,
Expand All @@ -23,8 +22,9 @@ local FILE_MAPPING = {
}

M.install = function()
vim.system({ "npm", "install", "--prefix", BASE_DIR }):wait()
vim.system({ "npm", "run", "build", "--prefix", BASE_DIR }):wait()
FS.copy_dir(BASE_DIR, SCRIPTS_BUILD_DIR)
vim.system({ "npm", "install", "--prefix", SCRIPTS_BUILD_DIR }):wait()
vim.system({ "npm", "run", "build", "--prefix", SCRIPTS_BUILD_DIR }):wait()
end

---@class Scripts
Expand Down
2 changes: 0 additions & 2 deletions lua/kulala/tmp/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion lua/kulala/tmp/scripts/requests/.gitignore

This file was deleted.

41 changes: 31 additions & 10 deletions lua/kulala/utils/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,19 @@ M.ps = M.get_path_separator()
---@vararg string
---@return string
M.join_paths = function(...)
if M.get_os() == "windows" then
if M.os == "windows" then
for _, v in ipairs({ ... }) do
-- if the path contains at least one forward slash,
-- then it needs to be converted to backslashes
if v:match("/") then
local parts = {}
for _, p in ipairs({ ... }) do
p = p:gsub("/", "\\")
table.insert(parts, p)
end
return table.concat(parts, M.ps)
end
end
return table.concat({ ... }, M.ps)
end
return table.concat({ ... }, M.ps)
Expand Down Expand Up @@ -167,6 +179,14 @@ M.file_exists = function(filename)
return vim.fn.filereadable(filename) == 1
end

M.copy_dir = function(source, destination)
if M.os == "unix" or M.os == "mac" then
vim.system({ "cp", "-r", source .. M.ps .. ".", destination }):wait()
elseif M.os == "windows" then
vim.system({ "xcopy", "/H", "/E", "/I", source .. M.ps .. "*", destination }):wait()
end
end

M.ensure_dir_exists = function(dir)
if vim.fn.isdirectory(dir) == 0 then
vim.fn.mkdir(dir, "p")
Expand All @@ -177,7 +197,9 @@ end
--- @return string
--- @usage local p = fs.get_plugin_tmp_dir()
M.get_plugin_tmp_dir = function()
local dir = M.join_paths(M.get_plugin_root_dir(), "tmp")
local cache = vim.fn.stdpath("cache")
---@cast cache string
local dir = M.join_paths(cache, "kulala")
M.ensure_dir_exists(dir)
return dir
end
Expand All @@ -187,6 +209,12 @@ M.get_scripts_dir = function()
return dir
end

M.get_tmp_scripts_build_dir = function()
local dir = M.join_paths(M.get_plugin_tmp_dir(), "scripts", "build")
M.ensure_dir_exists(dir)
return dir
end

M.get_tmp_scripts_dir = function()
local dir = M.join_paths(M.get_plugin_tmp_dir(), "scripts")
M.ensure_dir_exists(dir)
Expand Down Expand Up @@ -346,14 +374,7 @@ end
---Clears all cached files
M.clear_cached_files = function()
local tmp_dir = M.get_plugin_tmp_dir()
local scripts_dir = M.get_tmp_scripts_dir()
local request_scripts_dir = M.get_request_scripts_dir()
local compiled_pre_request_scripts = M.join_paths(M.get_scripts_dir(), "engines", "javascript", "lib", "dist")
local deleted_files = {}
deleted_files = vim.tbl_extend("force", deleted_files, M.delete_files_in_directory(tmp_dir))
deleted_files = vim.tbl_extend("force", deleted_files, M.delete_files_in_directory(scripts_dir))
deleted_files = vim.tbl_extend("force", deleted_files, M.delete_files_in_directory(request_scripts_dir))
deleted_files = vim.tbl_extend("force", deleted_files, M.delete_files_in_directory(compiled_pre_request_scripts))
local deleted_files = M.delete_files_in_directory(tmp_dir)
local string_list = vim.fn.join(
vim.tbl_map(function(file)
return "- " .. file
Expand Down
44 changes: 44 additions & 0 deletions scripts/install-ci-test-requirements.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
$Env:KULALA_ROOT_DIR = (Get-Location).Path

if ($Env:GH_CACHE_HIT -eq $null) {
mkdir .tests
}

cd .tests

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

if ($Env:GH_CACHE_HIT -eq $null) {
Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression
scoop install main/git
scoop install main/neovim@0.10.2
} else {
$Env:PATH = "$Env:USERPROFILE\scoop\shims;$Env:USERPROFILE\scoop\apps\git\current\cmd;$Env:USERPROFILE\scoop\apps\neovim\current\bin;$Env:PATH"
}

if ($Env:GH_CACHE_HIT -eq $null) {
Invoke-RestMethod -Uri https://github.com/mistweaverco/luajit-for-win64/archive/refs/tags/v0.0.2.zip -outfile luajit.zip
7z x luajit.zip
RM luajit.zip
cd luajit-for-win64-0.0.2
.\luajit-for-win64.cmd
} else {
cd luajit-for-win64-0.0.2
}

$Env:KULALA_LUA_DIR = (Get-Location).Path

$Env:PATH = "$Env:KULALA_LUA_DIR\tools\cmd;$Env:KULALA_LUA_DIR\tools\PortableGit\mingw64\bin;$Env:KULALA_LUA_DIR\tools\PortableGit\usr\bin;$Env:KULALA_LUA_DIR\tools\mingw\bin;$Env:KULALA_LUA_DIR\lib;$Env:KULALA_LUA_DIR\bin;$Env:APPDATA\LJ4W\LuaRocks\bin;$Env:PATH"
$Env:LUA_PATH = "$Env:KULALA_LUA_DIR\lua\?.lua;$Env:KULALA_LUA_DIR\lua\?\init.lua;$Env:APPDATA\luarocks\share\lua\5.1\?.lua;$Env:APPDATA\luarocks\share\lua\5.1\?\init.lua;$Env:LUA_PATH"
$Env:LUA_CPATH = "$Env:APPDATA\luarocks;$Env:APPDATA\luarocks\lib\lua\5.1\?.dll;$Env:LUA_CPATH"

if ($Env:GH_CACHE_HIT -eq $null) {
luarocks install --lua-version 5.1 busted
}

# Persist the Environment Variables
"PATH=$Env:Path" >> $Env:GITHUB_ENV
"LUA_PATH=$Env:LUA_PATH" >> $Env:GITHUB_ENV
"LUA_CPATH=$Env:LUA_CPATH" >> $Env:GITHUB_ENV

cd $Env:KULALA_ROOT_DIR
2 changes: 2 additions & 0 deletions scripts/tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nvim --version
nvim -l tests/minit.lua tests
46 changes: 44 additions & 2 deletions tests/_dockerfiles/windows/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
FROM mcr.microsoft.com/windows/nanoserver:ltsc2022
# FROM mcr.microsoft.com/windows/nanoserver:20H2-amd64
FROM mcr.microsoft.com/powershell:lts-windowsservercore-1809

SHELL ["pwsh", "-Command"]

USER ContainerAdministrator
WORKDIR C:\\Users\\ContainerAdministrator\\AppData\\Local\\nvim

WORKDIR "C:\\kulala.nvim"

RUN Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
RUN Invoke-RestMethod -Uri https://get.scoop.sh -outfile 'install.ps1'
RUN .\install.ps1 -RunAsAdmin

# required before add extras
RUN scoop install main/git

# add for extras suggested by neovim
RUN scoop bucket add extras

RUN scoop install extras/vcredist2022
RUN scoop install main/[email protected]

WORKDIR "C:\\luajjt"

RUN Invoke-RestMethod -Uri https://github.com/mistweaverco/luajit-for-win64/archive/refs/tags/v0.0.2.zip -outfile luajit.zip
RUN 7z x luajit.zip

WORKDIR "C:\\luajjt\luajit-for-win64-0.0.2"

RUN .\luajit-for-win64.cmd

RUN setx /M KULALA_LUA_DIR \"C:\luajjt\luajit-for-win64-0.0.2\"

RUN setx /M PATH \"$Env:KULALA_LUA_DIR\tools\cmd;$Env:KULALA_LUA_DIR\tools\PortableGit\mingw64\bin;$Env:KULALA_LUA_DIR\tools\PortableGit\usr\bin;$Env:KULALA_LUA_DIR\tools\mingw\bin;$Env:KULALA_LUA_DIR\lib;$Env:KULALA_LUA_DIR\bin;$Env:APPDATA\LJ4W\LuaRocks\bin;$Env:path\"

RUN setx /M LUA_PATH \"$Env:KULALA_LUA_DIR\lua\?.lua;$Env:KULALA_LUA_DIR\lua\?\init.lua;$Env:APPDATA\luarocks\share\lua\5.1\?.lua;$Env:APPDATA\luarocks\share\lua\5.1\?\init.lua;$Env:LUA_PATH\"
RUN setx /M LUA_CPATH \"$Env:APPDATA\luarocks;$Env:APPDATA\luarocks\lib\lua\5.1\?.dll;$Env:LUA_CPATH\"

RUN luarocks install --lua-version 5.1 busted

WORKDIR "C:\\kulala.nvim"

RUN git config --global safe.directory '*'
RUN git config --global core.autocrlf true

ENTRYPOINT ["pwsh"]
55 changes: 55 additions & 0 deletions tests/util/fs_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
local Fs = require("kulala.utils.fs")

local assert = require("luassert")

describe("kulala.utils.fs", function()
-- restore all changed done by luassert before each test run
local snapshot

before_each(function()
snapshot = assert:snapshot()
end)

after_each(function()
snapshot:revert()
end)

describe("join_paths on windows", function()
Fs.os = "windows"
Fs.ps = "\\"
it("joins mixed on windows", function()
local expected = "C:\\a\\b\\c"
local actual = Fs.join_paths("C:\\a", "b", "c")
assert.are.same(expected, actual)
end)
it("joins no-mixed on windows", function()
local expected = "C:\\a\\b\\c"
local actual = Fs.join_paths("C:\\a", "b", "c")
assert.are.same(expected, actual)
end)
it("fixes ps on windows", function()
local expected = "C:\\a\\user\\bin\\blah\\blubb"
local actual = Fs.join_paths("C:\\a", "user/bin", "blah/blubb")
assert.are.same(expected, actual)
end)
end)
describe("join_paths on linux", function()
Fs.os = "unix"
Fs.ps = "/"
it("joins mixed on unix", function()
local expected = "/a/b/c"
local actual = Fs.join_paths("/a", "b", "c")
assert.are.same(expected, actual)
end)
it("joins no-mixed on unix", function()
local expected = "/a/b/c"
local actual = Fs.join_paths("/a", "b", "c")
assert.are.same(expected, actual)
end)
it("joins more mixed on unix", function()
local expected = "/a/user/bin/blah/blubb"
local actual = Fs.join_paths("/a", "user/bin", "blah/blubb")
assert.are.same(expected, actual)
end)
end)
end)

0 comments on commit e1d6404

Please sign in to comment.