Skip to content

Commit 3ec83f9

Browse files
authored
feat: remove build logic from test clients (#24)
* feat: remove build logic from test clients * fix types
1 parent e74539f commit 3ec83f9

File tree

5 files changed

+94
-103
lines changed

5 files changed

+94
-103
lines changed

lua/neotest-vstest/client.lua

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,80 @@ local logger = require("neotest.logging")
33
local mtp_client = require("neotest-vstest.mtp")
44
local vstest_client = require("neotest-vstest.vstest")
55
local dotnet_utils = require("neotest-vstest.dotnet_utils")
6+
local files = require("neotest-vstest.files")
7+
8+
--- @class neotest-vstest.wrapper-client: neotest-vstest.Client
9+
--- @field project DotnetProjectInfo
10+
--- @field discover_tests_for_path fun(self: neotest-vstest.Client, path: string): table<string, table<string, neotest-vstest.TestCase>>
11+
--- @field private sub_client neotest-vstest.Client
12+
--- @field private semaphore nio.control.Semaphore
13+
--- @field private last_discovered integer
14+
local TestClient = {}
15+
TestClient.__index = TestClient
16+
17+
function TestClient:new(project, sub_client)
18+
local client = {
19+
sub_client = sub_client,
20+
project = project,
21+
semaphore = nio.control.semaphore(1),
22+
last_discovered = nil,
23+
}
24+
25+
setmetatable(client, self)
26+
return client
27+
end
28+
29+
function TestClient:discover_tests(path)
30+
self.semaphore.acquire()
31+
32+
local last_modified
33+
34+
local test_cases = self.sub_client.test_cases or {}
35+
36+
if self.last_discovered == nil then
37+
last_modified = dotnet_utils.get_project_last_modified(self.project)
38+
self.last_discovered = last_modified or 0
39+
test_cases = self.sub_client:discover_tests()
40+
else
41+
if path then
42+
last_modified = files.get_path_last_modified(path)
43+
else
44+
last_modified = dotnet_utils.get_project_last_modified(self.project)
45+
end
46+
47+
if last_modified and last_modified > self.last_discovered then
48+
logger.debug(
49+
"neotest-vstest: Discovering tests: "
50+
.. " last modified at "
51+
.. last_modified
52+
.. " last discovered at "
53+
.. self.last_discovered
54+
)
55+
dotnet_utils.build_project(self.project)
56+
last_modified = dotnet_utils.get_project_last_modified(self.project)
57+
self.last_discovered = last_modified or 0
58+
test_cases = self.sub_client:discover_tests()
59+
end
60+
end
61+
62+
self.semaphore.release()
63+
64+
return test_cases
65+
end
66+
67+
function TestClient:discover_tests_for_path(path)
68+
local tests = self:discover_tests(path)
69+
path = vim.fs.normalize(path)
70+
return tests[path]
71+
end
72+
73+
function TestClient:run_tests(ids)
74+
return self.sub_client:run_tests(ids)
75+
end
76+
77+
function TestClient:debug_tests(ids)
78+
return self.sub_client:debug_tests(ids)
79+
end
680

781
local client_discovery = {}
882

@@ -68,6 +142,8 @@ function client_discovery.get_client_for_project(project, solution)
68142
return
69143
end
70144

145+
client = TestClient:new(project, client)
146+
71147
clients[project.proj_file] = client
72148
return client
73149
end

lua/neotest-vstest/init.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ local function create_adapter(config)
5757

5858
solution = config.solution_selector and config.solution_selector(solutions) or nil
5959

60-
if solution then
61-
solution_dir = vim.fs.dirname(solution)
62-
solution_projects = dotnet_utils.projects(solution)
63-
return solution_dir
64-
else
65-
if #solutions > 0 then
66-
local solution_dir_future = nio.control.future()
60+
if solution or #solutions > 0 then
61+
local solution_dir_future = nio.control.future()
6762

63+
if solution then
64+
solution_dir = vim.fs.dirname(solution)
65+
solution_projects = dotnet_utils.projects(solution)
66+
solution_dir_future.set(solution_dir)
67+
else
6868
if #solutions == 1 then
6969
solution = solutions[1]
7070
solution_dir = vim.fs.dirname(solution)
@@ -90,6 +90,7 @@ local function create_adapter(config)
9090
if solution_dir_future.wait() and solution then
9191
logger.info(string.format("neotest-vstest: found solution file %s", solution))
9292
solution_projects = dotnet_utils.projects(solution)
93+
dotnet_utils.build_path(solution)
9394
return solution_dir
9495
end
9596
end

lua/neotest-vstest/mtp/init.lua

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,22 @@
1-
local nio = require("nio")
21
local logger = require("neotest.logging")
3-
local dotnet_utils = require("neotest-vstest.dotnet_utils")
4-
local files = require("neotest-vstest.files")
52
local mtp_client = require("neotest-vstest.mtp.client")
63

74
--- @class neotest-vstest.mtp-client: neotest-vstest.Client
85
--- @field project DotnetProjectInfo
9-
--- @field semaphore nio.control.Semaphore
10-
--- @field last_discovered integer
6+
--- @field private last_discovered integer
117
local Client = {}
128
Client.__index = Client
139

14-
local clients = {}
15-
1610
---@param project DotnetProjectInfo
1711
function Client:new(project)
18-
if clients[project.proj_file] then
19-
logger.info("neotest-vstest: Reusing existing (MTP) client for: " .. vim.inspect(project))
20-
return clients[project.proj_file]
21-
end
22-
2312
logger.info("neotest-vstest: Creating new (MTP) client for: " .. vim.inspect(project))
2413
local client = {
2514
project = project,
2615
test_cases = {},
2716
last_discovered = 0,
28-
semaphore = nio.control.semaphore(1),
2917
}
3018
setmetatable(client, self)
3119

32-
clients[project.proj_file] = client
33-
3420
return client
3521
end
3622

@@ -61,42 +47,13 @@ local function map_test_cases(project, test_nodes)
6147
return test_cases
6248
end
6349

64-
function Client:discover_tests(path)
65-
self.semaphore.acquire()
66-
67-
local last_modified
68-
if path then
69-
last_modified = files.get_path_last_modified(path)
70-
else
71-
last_modified = dotnet_utils.get_project_last_modified(self.project)
72-
end
73-
if last_modified and last_modified > self.last_discovered then
74-
logger.debug(
75-
"neotest-vstest: Discovering tests: "
76-
.. " last modified at "
77-
.. last_modified
78-
.. " last discovered at "
79-
.. self.last_discovered
80-
)
81-
dotnet_utils.build_project(self.project)
82-
last_modified = dotnet_utils.get_project_last_modified(self.project)
83-
self.last_discovered = last_modified or 0
84-
self.test_nodes = mtp_client.discovery_tests(self.project.dll_file)
85-
self.test_cases = map_test_cases(self.project, self.test_nodes)
86-
logger.debug(self.test_cases)
87-
end
88-
89-
self.semaphore.release()
50+
function Client:discover_tests()
51+
self.test_nodes = mtp_client.discovery_tests(self.project.dll_file)
52+
self.test_cases = map_test_cases(self.project, self.test_nodes)
9053

9154
return self.test_cases
9255
end
9356

94-
function Client:discover_tests_for_path(path)
95-
self:discover_tests(path)
96-
path = vim.fs.normalize(path)
97-
return self.test_cases[path]
98-
end
99-
10057
---@async
10158
---@param ids string[] list of test ids to run
10259
---@return neotest-vstest.Client.RunResult

lua/neotest-vstest/types.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
---@field stop fun()
2020

2121
---@class neotest-vstest.Client
22+
---@field test_cases table<string, table<string, neotest-vstest.TestCase>>
23+
---@field discover_tests fun(self: neotest-vstest.Client): table<string, table<string, neotest-vstest.TestCase>>
2224
---@field run_tests fun(self: neotest-vstest.Client, ids: string|string[]): neotest-vstest.Client.RunResult
23-
---@field discover_tests fun(self: neotest-vstest.Client, path?: string): table<string, table<string, neotest-vstest.TestCase>>
24-
---@field discover_tests_for_path fun(self: neotest-vstest.Client, path: string): table<string, table<string, neotest-vstest.TestCase>>
2525
---@field debug_tests fun(self: neotest-vstest.Client, ids: string|string[]): neotest-vstest.Client.DebugResult

lua/neotest-vstest/vstest/init.lua

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
local nio = require("nio")
22
local lib = require("neotest.lib")
33
local logger = require("neotest.logging")
4-
local dotnet_utils = require("neotest-vstest.dotnet_utils")
54
local cli_wrapper = require("neotest-vstest.vstest.cli_wrapper")
6-
local files = require("neotest-vstest.files")
75
local utilities = require("neotest-vstest.utilities")
86
local vstest_client = require("neotest-vstest.vstest.client")
97

108
--- @class neotest-vstest.vstest-client: neotest-vstest.Client
119
--- @field project DotnetProjectInfo
12-
--- @field semaphore nio.control.Semaphore
13-
--- @field test_runner { execute: function, stop: function }
10+
--- @field private test_runner { execute: function, stop: function }
1411
local Client = {}
1512
Client.__index = Client
1613
Client.__gc = function(self)
@@ -19,67 +16,27 @@ Client.__gc = function(self)
1916
end
2017
end
2118

22-
local clients = {}
23-
2419
---@param project DotnetProjectInfo
2520
function Client:new(project)
26-
if clients[project.proj_file] then
27-
logger.info("neotest-vstest: Reusing existing (vstest) client for: " .. vim.inspect(project))
28-
return clients[project.proj_file]
29-
end
30-
3121
logger.info("neotest-vstest: Creating new (vstest) client for: " .. vim.inspect(project))
3222
local client = {
3323
project = project,
3424
test_cases = {},
3525
last_discovered = 0,
36-
semaphore = nio.control.semaphore(1),
3726
test_runner = cli_wrapper.create_test_runner(project),
3827
}
3928
setmetatable(client, self)
4029

41-
clients[project.proj_file] = client
42-
4330
return client
4431
end
4532

46-
function Client:discover_tests(path)
47-
self.semaphore.acquire()
48-
49-
local last_modified
50-
if path then
51-
last_modified = files.get_path_last_modified(path)
52-
else
53-
last_modified = dotnet_utils.get_project_last_modified(self.project)
54-
end
55-
if last_modified and last_modified > self.last_discovered then
56-
logger.debug(
57-
"neotest-vstest: Discovering tests: "
58-
.. " last modified at "
59-
.. last_modified
60-
.. " last discovered at "
61-
.. self.last_discovered
62-
)
63-
dotnet_utils.build_project(self.project)
64-
last_modified = dotnet_utils.get_project_last_modified(self.project)
65-
self.last_discovered = last_modified or 0
66-
self.test_cases = vstest_client.discover_tests_in_project(
67-
self.test_runner.execute,
68-
self.project
69-
) or {}
70-
end
71-
72-
self.semaphore.release()
33+
function Client:discover_tests()
34+
self.test_cases = vstest_client.discover_tests_in_project(self.test_runner.execute, self.project)
35+
or {}
7336

7437
return self.test_cases
7538
end
7639

77-
function Client:discover_tests_for_path(path)
78-
self:discover_tests(path)
79-
path = vim.fs.normalize(path)
80-
return self.test_cases[path]
81-
end
82-
8340
---@async
8441
---@param ids string[] list of test ids to run
8542
---@return neotest-vstest.Client.RunResult

0 commit comments

Comments
 (0)