Skip to content

Commit

Permalink
Add testing
Browse files Browse the repository at this point in the history
* improve position detection

* add tests

* update workflow

* add treesitter cli to workflow

* bump lower bound nvim version

* test on all os

* set dotnet version
  • Loading branch information
Nsidorenco committed Dec 4, 2024
1 parent 109dd72 commit c5c6e10
Show file tree
Hide file tree
Showing 20 changed files with 388 additions and 65 deletions.
13 changes: 13 additions & 0 deletions .busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
return {
_all = {
coverage = false,
lpath = "lua/?.lua;lua/?/init.lua",
lua = "nlua",
},
default = {
verbose = true,
},
tests = {
verbose = true,
},
}
59 changes: 27 additions & 32 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,48 @@ jobs:
version: latest
args: --check .

#documentation:
# runs-on: ubuntu-latest
# name: documentation
# steps:
# - uses: actions/checkout@v3
#
# - name: setup neovim
# uses: rhysd/action-setup-vim@v1
# with:
# neovim: true
# version: v0.8.2

# - name: generate documentation
# run: make documentation-ci

# - name: check docs diff
# run: exit $(git diff --name-only origin/main -- doc | wc -l)

tests:
needs:
needs:
- lint
#- documentation
runs-on: ubuntu-latest
timeout-minutes: 2
strategy:
matrix:
neovim_version: ['v0.9.1', 'v0.9.4', 'v0.10.0', 'nightly']
os: [ubuntu-latest, macos-latest, windows-latest]
neovim_version: ["v0.10.0"]
include:
- os: ubuntu-latest
neovim_version: "nightly"

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: "9.0.x"

- run: date +%F > todays-date
- name: Install C/C++ Compiler
uses: rlalik/setup-cpp-compiler@master
with:
compiler: clang-latest

- name: restore cache for today's nightly.
uses: actions/cache@v3
- name: Install tree-sitter CLI
uses: baptiste0928/cargo-install@v3
with:
path: _neovim
key: ${{ runner.os }}-x64-${{ hashFiles('todays-date') }}
crate: tree-sitter-cli

- name: setup neovim
uses: rhysd/action-setup-vim@v1
- name: Run tests
id: test
uses: nvim-neorocks/nvim-busted-action@v1
with:
neovim: true
version: ${{ matrix.neovim_version }}
nvim_version: ${{ matrix.neovim_version }}

- name: run tests
run: make test-ci
- name: Save neotest log
if: always() && steps.test.outcome == 'failure'
uses: actions/upload-artifact@v4
with:
name: neotest-log-${{ matrix.neovim_version }}-${{ matrix.os }}
path: ~/.local/state/nvim/neotest.log

release:
name: release
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ luac.out
# Test dependencies
deps/
**/obj/*
/luarocks
/lua
/lua_modules
/.luarocks

obj/
bin/
10 changes: 10 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ignore = {
"631", -- max_line_length
"122", -- read-only field of global variable
}
read_globals = {
"vim",
"describe",
"it",
"assert",
}
12 changes: 1 addition & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@ all:

# runs all the test files.
test:
nvim --version | head -n 1 && echo ''
./tests/test.sh

# installs `mini.nvim`, used for both the tests and documentation.
deps:
@mkdir -p deps
git clone --depth 1 https://github.com/echasnovski/mini.doc.git deps/mini.doc.nvim
git clone --depth 1 https://github.com/nvim-neotest/neotest.git deps/neotest
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim.git deps/plenary
git clone --depth 1 https://github.com/nvim-treesitter/nvim-treesitter.git deps/nvim-treesitter
git clone --depth 1 https://github.com/nvim-neotest/nvim-nio deps/nvim-nio
luarocks test --local

# installs deps before running tests, useful for the CI.
test-ci: deps test
Expand Down
10 changes: 5 additions & 5 deletions lua/neotest-dotnet/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ end
DotnetNeotestAdapter.discover_positions = function(path)
logger.info(string.format("scanning %s for tests...", path))

local fsharp_query = require("neotest-dotnet.queries.fsharp")
local c_sharp_query = require("neotest-dotnet.queries.c_sharp")

local filetype = (vim.endswith(path, ".fs") and "fsharp") or "c_sharp"

local tests_in_file = vstest.discover_tests(path)
Expand All @@ -94,8 +91,11 @@ DotnetNeotestAdapter.discover_positions = function(path)

local root = lib.treesitter.fast_parse(lang_tree):root()

local query =
lib.treesitter.normalise_query(lang, filetype == "fsharp" and fsharp_query or c_sharp_query)
local query = lib.treesitter.normalise_query(
lang,
filetype == "fsharp" and require("neotest-dotnet.queries.fsharp")
or require("neotest-dotnet.queries.c_sharp")
)

local sep = lib.files.sep
local path_elems = vim.split(path, sep, { plain = true })
Expand Down
33 changes: 21 additions & 12 deletions lua/neotest-dotnet/vstest_wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,19 @@ end

local function get_script(script_name)
local script_paths = vim.api.nvim_get_runtime_file(script_name, true)
logger.debug("possible scripts:")
logger.debug(script_paths)
for _, path in ipairs(script_paths) do
if vim.endswith(path, ("neotest-dotnet%s" .. script_name):format(lib.files.sep)) then
return path
end
end
end

local proj_file_path_map = {}

---collects project information based on file
---@param path string
---@return { proj_file: string, dll_file: string, proj_dir: string }
function M.get_proj_info(path)
if proj_file_path_map[path] then
return proj_file_path_map[path]
end

local proj_file = vim.fs.find(function(name, _)
return name:match("%.[cf]sproj$")
end, { upward = true, type = "file", path = vim.fs.dirname(path) })[1]
Expand All @@ -70,7 +66,6 @@ function M.get_proj_info(path)
proj_dir = dir_name,
}

proj_file_path_map[path] = proj_data
return proj_data
end

Expand All @@ -83,9 +78,12 @@ local function invoke_test_runner(command)
return
end

local test_discovery_script = get_script("run_tests.fsx")
local test_discovery_script = get_script("scripts/run_tests.fsx")
local testhost_dll = get_vstest_path()

logger.debug("found discovery script: " .. test_discovery_script)
logger.debug("found testhost dll: " .. testhost_dll)

local vstest_command = { "dotnet", "fsi", test_discovery_script, testhost_dll }

logger.info("starting vstest console with:")
Expand All @@ -94,8 +92,12 @@ local function invoke_test_runner(command)
local process = vim.system(vstest_command, {
stdin = true,
stdout = function(err, data)
logger.trace(data)
logger.trace(err)
if data then
logger.trace(data)
end
if err then
logger.trace(err)
end
end,
}, function(obj)
logger.warn("vstest process died :(")
Expand Down Expand Up @@ -165,7 +167,7 @@ function M.discover_tests(path)
local json
local proj_info = M.get_proj_info(path)

if not (proj_info.proj_file and proj_info.dll_file) then
if not proj_info.proj_file then
logger.warn(string.format("failed to find project file for %s", path))
return {}
end
Expand All @@ -189,6 +191,13 @@ function M.discover_tests(path)
logger.debug(stdout)
end

proj_info = M.get_proj_info(path)

if not proj_info.dll_file then
logger.warn(string.format("failed to find project dll for %s", path))
return {}
end

local dll_open_err, dll_stats = nio.uv.fs_stat(proj_info.dll_file)
assert(not dll_open_err, dll_open_err)

Expand Down Expand Up @@ -286,7 +295,7 @@ function M.discover_tests(path)

logger.debug("Waiting for result file to populated...")

local max_wait = 30 * 1000 -- 30 sec
local max_wait = 60 * 1000 -- 60 sec

local done = M.spin_lock_wait_file(wait_file, max_wait)
if done then
Expand Down
27 changes: 27 additions & 0 deletions neotest-dotnet-scm-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
rockspec_format = "3.0"
package = "neotest-dotnet"
version = "scm-1"

dependencies = {
"lua >= 5.1",
"neotest",
"tree-sitter-fsharp",
"tree-sitter-c_sharp",
}

test_dependencies = {
"lua >= 5.1",
"busted",
"nlua",
}

source = {
url = "git://github.com/issafalcon/neotest-dotnet",
}

build = {
type = "builtin",
copy_directories = {
"scripts",
},
}
12 changes: 7 additions & 5 deletions run_tests.fsx → scripts/run_tests.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ module TestDiscovery =
ValueOption.None

let logHandler (level: TestMessageLevel) (message: string) =
if level = TestMessageLevel.Error then
Console.Error.WriteLine(message)
else
Console.WriteLine(message)
if not <| String.IsNullOrWhiteSpace message then
if level = TestMessageLevel.Error then
Console.Error.WriteLine(message)
else
Console.WriteLine(message)

type TestCaseDto =
{ CodeFilePath: string
Expand Down Expand Up @@ -170,7 +171,7 @@ module TestDiscovery =

member _.AttachDebuggerToProcess(pid: int, ct: CancellationToken) =
use cts = CancellationTokenSource.CreateLinkedTokenSource(ct)
cts.CancelAfter(TimeSpan.FromSeconds(450))
cts.CancelAfter(TimeSpan.FromSeconds(450.))

do
Console.WriteLine($"spawned test process with pid: {pid}")
Expand Down Expand Up @@ -234,6 +235,7 @@ module TestDiscovery =
PlaygroundTestDiscoveryHandler() :> ITestDiscoveryEventsHandler2

for source in args.Sources do
Console.WriteLine($"Discovering tests for: {source}")
r.DiscoverTests([| source |], sourceSettings, options, testSession, discoveryHandler)

use testsWriter = new StreamWriter(args.OutputPath, append = false)
Expand Down
12 changes: 12 additions & 0 deletions spec/installation_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
describe("Test environment", function()
it("Test can access vim namespace", function()
assert(vim, "Cannot access vim namespace")
assert.are.same(vim.trim(" a "), "a")
end)
it("Test can access neotest dependency", function()
assert(require("neotest"), "neotest")
end)
it("Test can access module in lua/neotest-dotnet", function()
assert(require("neotest-dotnet"), "Could not access main module")
end)
end)
20 changes: 20 additions & 0 deletions spec/root_detection_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
describe("Test root detection", function()
it("Detect .sln file as root", function()
local plugin = require("neotest-dotnet")
local dir = vim.fn.getcwd() .. "/spec/samples/test_solution"
local root = plugin.root(dir)
assert.are_equal(dir, root)
end)
it("Detect .sln file as root from project dir", function()
local plugin = require("neotest-dotnet")
local dir = vim.fn.getcwd() .. "/spec/samples/test_solution"
local root = plugin.root(dir .. "/src/FsharpTest")
assert.are_equal(dir, root)
end)
it("Detect .fsproj file as root from project dir with no .sln file", function()
local plugin = require("neotest-dotnet")
local dir = vim.fn.getcwd() .. "/spec/samples/test_project"
local root = plugin.root(dir)
assert.are_equal(dir, root)
end)
end)
Empty file.
34 changes: 34 additions & 0 deletions spec/samples/test_solution/fsharp-test.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{364BD0DC-1C6E-4811-BC58-D543DB1E67D2}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FsharpTest", "src\FsharpTest\FsharpTest.fsproj", "{FFB89E81-0B57-4A30-9836-DC83EFD2ADA3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpTest", "src\CSharpTest\CSharpTest.csproj", "{D0B0861B-D9E5-4EA5-8CAE-0CDCF0054021}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FFB89E81-0B57-4A30-9836-DC83EFD2ADA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FFB89E81-0B57-4A30-9836-DC83EFD2ADA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FFB89E81-0B57-4A30-9836-DC83EFD2ADA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FFB89E81-0B57-4A30-9836-DC83EFD2ADA3}.Release|Any CPU.Build.0 = Release|Any CPU
{D0B0861B-D9E5-4EA5-8CAE-0CDCF0054021}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0B0861B-D9E5-4EA5-8CAE-0CDCF0054021}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0B0861B-D9E5-4EA5-8CAE-0CDCF0054021}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0B0861B-D9E5-4EA5-8CAE-0CDCF0054021}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{FFB89E81-0B57-4A30-9836-DC83EFD2ADA3} = {364BD0DC-1C6E-4811-BC58-D543DB1E67D2}
{D0B0861B-D9E5-4EA5-8CAE-0CDCF0054021} = {364BD0DC-1C6E-4811-BC58-D543DB1E67D2}
EndGlobalSection
EndGlobal
Loading

0 comments on commit c5c6e10

Please sign in to comment.