Skip to content

Commit

Permalink
Added command to run test target + build for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-kulik committed Nov 29, 2023
1 parent 23c4a1c commit 2066714
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 22 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,11 @@ Xcodebuild.nvim comes with the following commands:
| `XcodebuildBuild` | Build project |
| `XcodebuildCleanBuild` | Build project (clean build) |
| `XcodebuildBuildRun` | Build & run app |
| `XcodebuildBuildForTesting` | Build for testing |
| `XcodebuildRun` | Run app without building |
| `XcodebuildCancel` | Cancel currently running action |
| `XcodebuildTest` | Run tests (whole test plan) |
| `XcodebuildTestTarget` | Run test target (where the cursor is) |
| `XcodebuildTestClass` | Run test class (where the cursor is) |
| `XcodebuildTestFunc` | Run test (where the cursor is) |
| `XcodebuildTestSelected` | Run selected tests (using visual mode) |
Expand Down Expand Up @@ -306,6 +308,7 @@ vim.keymap.set("n", "<leader>xb", "<cmd>XcodebuildBuild<cr>", { desc = "Build Pr
vim.keymap.set("n", "<leader>xr", "<cmd>XcodebuildBuildRun<cr>", { desc = "Build & Run Project" })
vim.keymap.set("n", "<leader>xt", "<cmd>XcodebuildTest<cr>", { desc = "Run Tests" })
vim.keymap.set("n", "<leader>xT", "<cmd>XcodebuildTestClass<cr>", { desc = "Run This Test Class" })
vim.keymap.set("n", "<leader>xf", "<cmd>XcodebuildTestTarget<cr>", { desc = "Run This Test Target" })
vim.keymap.set("n", "<leader>X", "<cmd>XcodebuildPicker<cr>", { desc = "Show All Xcodebuild Actions" })
vim.keymap.set("n", "<leader>xd", "<cmd>XcodebuildSelectDevice<cr>", { desc = "Select Device" })
vim.keymap.set("n", "<leader>xp", "<cmd>XcodebuildSelectTestPlan<cr>", { desc = "Select Test Plan" })
Expand Down
12 changes: 12 additions & 0 deletions lua/xcodebuild/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ function M.clean_build(callback)
coordinator.build_project({ clean = true }, callback)
end

function M.build_for_testing(callback)
coordinator.cancel()
coordinator.build_project({ buildForTesting = true }, callback)
end

function M.cancel()
coordinator.cancel()
notifications.send("Stopped")
Expand All @@ -74,6 +79,13 @@ function M.run_tests()
coordinator.run_tests()
end

function M.run_target_tests()
coordinator.cancel()
coordinator.run_selected_tests({
currentTarget = true,
})
end

function M.run_class_tests()
coordinator.cancel()
coordinator.run_selected_tests({
Expand Down
23 changes: 18 additions & 5 deletions lua/xcodebuild/coordinator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,32 @@ function M.run_selected_tests(opts)
return
end

local selectedClass, selectedTests = find_tests(opts)
local selectedClass, selectedTests
if not opts.currentTarget then
selectedClass, selectedTests = find_tests(opts)
end

local start = function()
local testsToRun = {}
local testFilepath = vim.api.nvim_buf_get_name(0)
local target = testSearch.find_target_for_file(testFilepath)

if not target then
notifications.send_error("Could not detect test target. Please run build again.")
if opts.doNotBuild then
notifications.send_error("Could not detect test target. Please run build for testing.")
else
opts.doNotBuild = true
M.build_project({ buildForTesting = true }, function()
M.run_selected_tests(opts)
end)
end
return
end

if opts.currentTarget then
table.insert(testsToRun, target)
end

if opts.currentClass and selectedClass then
table.insert(testsToRun, target .. "/" .. selectedClass)
end
Expand Down Expand Up @@ -429,9 +443,8 @@ function M.run_selected_tests(opts)

if util.is_empty(testSearch.targetsFilesMap) then
notifications.send("Loading tests...")
M.currentJobId = M.build_project({
buildForTesting = true,
}, function()
M.currentJobId = M.build_project({ buildForTesting = true }, function()
opts.doNotBuild = true
testSearch.load_targets_map()
start()
end)
Expand Down
2 changes: 2 additions & 0 deletions lua/xcodebuild/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ function M.setup(options)
vim.api.nvim_create_user_command("XcodebuildBuild", call(actions.build), { nargs = 0 })
vim.api.nvim_create_user_command("XcodebuildCleanBuild", call(actions.clean_build), { nargs = 0 })
vim.api.nvim_create_user_command("XcodebuildBuildRun", call(actions.build_and_run), { nargs = 0 })
vim.api.nvim_create_user_command("XcodebuildBuildForTesting", call(actions.build_for_testing), { nargs = 0 })
vim.api.nvim_create_user_command("XcodebuildRun", call(actions.run), { nargs = 0 })
vim.api.nvim_create_user_command("XcodebuildCancel", call(actions.cancel), { nargs = 0 })

-- Testing
vim.api.nvim_create_user_command("XcodebuildTest", call(actions.run_tests), { nargs = 0 })
vim.api.nvim_create_user_command("XcodebuildTestTarget", call(actions.run_target_tests), { nargs = 0 })
vim.api.nvim_create_user_command("XcodebuildTestClass", call(actions.run_class_tests), { nargs = 0 })
vim.api.nvim_create_user_command("XcodebuildTestFunc", call(actions.run_func_test), { nargs = 0 })
vim.api.nvim_create_user_command("XcodebuildTestSelected", call(actions.run_selected_tests), { nargs = 0 })
Expand Down
18 changes: 8 additions & 10 deletions lua/xcodebuild/notifications.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ function M.send_build_started(buildForTesting)
local lastBuildTime = projectConfig.settings.lastBuildTime

buildState.id = math.random(10000000)
buildState.timer = not buildForTesting and M.start_action_timer("Building", lastBuildTime)
buildState.timer =
M.start_action_timer(buildForTesting and "Building for Testing" or "Building", lastBuildTime)
buildState.startTime = os.time()
buildState.buildForTesting = buildForTesting

return buildState.id
end
Expand All @@ -93,14 +93,12 @@ function M.send_build_finished(report, id, isCancelled)
if isCancelled then
M.send_warning("Build cancelled")
elseif util.is_empty(report.buildErrors) then
if not buildState.buildForTesting then
local duration = buildState.buildDuration
local projectConfig = require("xcodebuild.project_config")
projectConfig.settings.lastBuildTime = duration
projectConfig.save_settings()
local duration = buildState.buildDuration
local projectConfig = require("xcodebuild.project_config")
projectConfig.settings.lastBuildTime = duration
projectConfig.save_settings()

M.send(string.format("Build Succeeded [%d seconds]", duration))
end
M.send(string.format("Build Succeeded [%d seconds]", duration))
else
M.send_error("Build Failed [" .. #report.buildErrors .. " error(s)]")
end
Expand All @@ -114,7 +112,7 @@ end

function M.show_tests_progress(report)
if not next(report.tests) then
M.send_progress("Building Project...")
M.send_progress("Starting Tests...")
else
M.send_progress(
"Running Tests [Executed: " .. report.testsCount .. ", Failed: " .. report.failedTestsCount .. "]"
Expand Down
18 changes: 11 additions & 7 deletions lua/xcodebuild/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,12 @@ function M.show_all_actions()
"Build Project",
"Build Project (Clean Build)",
"Build & Run Project",
"Build For Testing",
"Run Without Building",
"Cancel Running Action",

"Run Test Plan (All Tests)",
"Run This Test Target",
"Run This Test Class",
"Run This Test",
"Run Selected Tests",
Expand All @@ -389,10 +391,12 @@ function M.show_all_actions()
actions.build,
actions.clean_build,
actions.build_and_run,
actions.build_for_testing,
actions.run,
actions.cancel,

actions.run_tests,
actions.run_target_tests,
actions.run_class_tests,
actions.run_func_test,
actions.run_selected_tests,
Expand Down Expand Up @@ -421,23 +425,23 @@ function M.show_all_actions()

if config.prepare_snapshot_test_previews then
if util.is_not_empty(snapshots.get_failing_snapshots()) then
table.insert(actionsNames, 11, "Preview Failing Snapshot Tests")
table.insert(actionsPointers, 11, actions.show_failing_snapshot_tests)
table.insert(actionsNames, 13, "Preview Failing Snapshot Tests")
table.insert(actionsPointers, 13, actions.show_failing_snapshot_tests)
end
end

if config.code_coverage.enabled then
table.insert(actionsNames, 11, "Toggle Code Coverage")
table.insert(actionsPointers, 11, actions.toggle_code_coverage)
table.insert(actionsNames, 13, "Toggle Code Coverage")
table.insert(actionsPointers, 13, actions.toggle_code_coverage)

if require("xcodebuild.coverage").is_code_coverage_available() then
table.insert(actionsNames, 12, "Show Code Coverage Report")
table.insert(actionsPointers, 12, actions.show_code_coverage_report)
table.insert(actionsNames, 14, "Show Code Coverage Report")
table.insert(actionsPointers, 14, actions.show_code_coverage_report)
end
end

M.show("Xcodebuild Actions", actionsNames, function(_, index)
if index > 10 or #actionsNames == 1 then
if index > 12 or #actionsNames == 1 then
actionsPointers[index]()
else
vim.defer_fn(actionsPointers[index], 100)
Expand Down

0 comments on commit 2066714

Please sign in to comment.