Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/lua
- local has_posix, posix = pcall(require, "posix")
- --[[
- We now have a new concern:
- Sometimes we want a to copy
- a terminal command, a test command, or a tmux command
- the distinctions between these are currently to be determined.
- what we do know for know is that all operations with respect to the system clipboard will reside one level of abstraction below the X11 object.
- as a matter of fact, a new object, SystemClipboard, will be a concrete class that will be used in X11
- ]]
- function TypeOfFile(file)
- return {
- check = function(self)
- local file = self:activeTestFile(file)
- local fileContainsTestCode = self:containsTestCode(file)
- local programmingLanguageConcerningFile = self:programmingLanguage(file)
- return {
- FilePath = file,
- ProgrammingLanguage = programmingLanguageConcerningFile,
- IsTestCode = fileContainsTestCode,
- }
- end,
- activeTestFile = function(self, filePath)
- local file = io.open(filePath, "r")
- local content = file:read("*a")
- file:close()
- return content
- end,
- containsTestCode = function(self, filePath)
- local fileName = string.match(filePath, "[^/]+$")
- if fileName and string.find(fileName, "test") then
- return true
- else
- return false
- end
- end,
- programmingLanguage = function(self)
- local extensionToLanguage = {
- [".sh"] = "bash",
- [".bash"] = "bash",
- [".js"] = "javascript",
- [".mjs"] = "javascript",
- [".cjs"] = "javascript",
- [".java"] = "java",
- [".py"] = "python",
- [".lua"] = "lua",
- [".t"] = "perl",
- }
- -- Extract file extension
- local extension = file:match("%.([^%.]+)$")
- if extension then
- extension = "." .. extension
- return extensionToLanguage[extension] or "Unknown"
- else
- return "Unknown"
- end
- end,
- }
- end
- function AvailableTestingFrameworks()
- return {
- selectFrameworkFor = function(self, language)
- local testFrameworks = self:testFrameworks()
- return self:testingFrameworkRegardingLanguage(testFrameworks, language)
- end,
- testFrameworks = function(self)
- return {
- { "perl", { "prove -vv " } },
- { "lua", { "busted" } },
- { "javascript", { "mocha ", "jest " } },
- { "go", { "go test -v " } },
- { "c++", { "gtest ", "catch2 " } },
- { "php", { "phpunit " } },
- { "ruby", { "rspec ", "minitest " } },
- { "rust", { "cargo test -- --nocapture " } },
- { "java", { "mvn test ", "gradle test " } },
- { "c#", { "dotnet test " } },
- { "bash", { "bats " } },
- { "r", { "testthat::test_dir() " } },
- { "haskell", { "stack test ", "cabal test " } },
- }
- end,
- testingFrameworkRegardingLanguage = function(self, availableFrameworks, languageWithRespectToTestFile)
- for i, framework in ipairs(availableFrameworks) do
- if framework[1] == languageWithRespectToTestFile then
- return framework[2][1]
- end
- end
- return nil
- end,
- }
- end
- function TestCommand(dependencies)
- return {
- toBeExecutedInTMUX = function(self)
- local file = self:fileProperties():check()
- local testingFrameworkCommand = self:availableFrameworks():selectFrameworkFor(file.ProgrammingLanguage)
- return self:constructedTerminalCommand(testingFrameworkCommand, file.FilePath)
- end,
- fileProperties = function(self)
- return dependencies[1]
- end,
- availableFrameworks = function(self)
- return dependencies[2]
- end,
- constructedTerminalCommand = function(self, testingFrameworkCommand, testFile)
- return string.format(
- "%s %s",
- -- "%s %s && listenForTabKey",
- testingFrameworkCommand:gsub("[\r\n]", ""),
- testFile:gsub("[\r\n]", "")
- )
- end,
- }
- end
- function TabKey()
- return {
- hasBeenPressedByUser = function(self)
- if not has_posix then
- return false
- end
- -- Check if input is available without blocking
- local fd = posix.unistd.STDIN_FILENO
- local readfds = posix.sys.select({ fd }, {}, {}, 0)
- if readfds and #readfds > 0 then
- local char = io.read(1)
- return char and char:byte() == 9
- end
- return false
- end,
- }
- end
- function X11(tabKey)
- return {
- userWantsToGoBackToProgrammingTab = function(self)
- while true do
- if tabKey:hasBeenPressedByUser() then
- return true
- end
- posix.unistd.sleep(1)
- end
- end,
- execute = function(self, action)
- local commandType = action[1]
- if commandType == "copy" then
- self:copyCommandToClipboard(action[2])
- elseif commandType == "paste" then
- self:pasteCommandInTerminal()
- elseif commandType == "terminalCommand" then
- self:executeCommandInTerminal()
- end
- end,
- copyCommandToClipboard = function(self, testCommand)
- -- Use io.popen to write directly to xsel
- local pipe = io.popen("xsel -ib", "w")
- if pipe then
- pipe:write(testCommand)
- pipe:close()
- else
- end
- end,
- pasteCommandInTerminal = function(self)
- os.execute("sleep 0.01")
- os.execute("xdotool key ctrl+shift+v")
- end,
- executeCommandInTerminal = function(self)
- os.execute("sleep 0.01")
- os.execute("xdotool key Return")
- end,
- }
- end
- function Terminal(desktopEnvironment)
- return {
- executeCommandsGiven = function(self, testCommand)
- self:changeTheTerminalDirectoryToTestFileDirectory(testCommand)
- self:clearScreenBeforeExecutingTestCommand()
- self:executeTestInTerminalTabAfterEnteringCommand(testCommand)
- end,
- changeTheTerminalDirectoryToTestFileDirectory = function(self, testCommand)
- local testDirectory = TestDirectory(testCommand)
- ChangeDirectory(desktopEnvironment):given(testDirectory)
- end,
- clearScreenBeforeExecutingTestCommand = function(self)
- os.execute("xdotool key ctrl+l")
- end,
- executeTestInTerminalTabAfterEnteringCommand = function(self, testCommand)
- desktopEnvironment:execute({ "copy", testCommand })
- desktopEnvironment:execute({ "paste" })
- desktopEnvironment:execute({ "terminalCommand" })
- end,
- }
- end
- function TestDirectory(command)
- return {
- withRespectToTestFile = function(self)
- local testFile = self:testFileAbsoluteFilePath()
- return self:testDirectoryPath(testFile)
- end,
- testFileAbsoluteFilePath = function(self)
- local parts = {}
- for part in string.gmatch(command, "[^%s]+") do
- table.insert(parts, part)
- end
- return parts[2]
- end,
- testDirectoryPath = function(self, testFile)
- return string.match(testFile, ".*/")
- end,
- }
- end
- function ChangeDirectory(desktopEnvironment)
- return {
- given = function(self, testDirectory)
- local cdCommand = string.format("cd %s", testDirectory:withRespectToTestFile())
- self:enterChangeDirectoryCommand(cdCommand)
- self:executeChangeDirectoryCommand()
- end,
- enterChangeDirectoryCommand = function(self, cdCommand)
- desktopEnvironment:execute({ "copy", cdCommand })
- os.execute("sleep 0.01")
- desktopEnvironment:execute({ "paste" })
- os.execute("sleep 0.01")
- end,
- executeChangeDirectoryCommand = function(self)
- os.execute("xdotool key return")
- os.execute("sleep 0.01")
- end,
- }
- end
- function TMUX(tmuxDependencies)
- return {
- executeTestCommandInTestingTabGiven = function(self, testCommand)
- local testCommand = testCommand:toBeExecutedInTMUX()
- self:moveToTestTab()
- self:closeProcessesInTestTab()
- tmuxDependencies.Terminal:executeCommandsGiven(testCommand)
- if tmuxDependencies.DesktopEnvironment:userWantsToGoBackToProgrammingTab() then
- self:moveToProgrammingTab()
- end
- end,
- moveToTestTab = function(self)
- os.execute("tmux -L default next-window")
- end,
- closeProcessesInTestTab = function(self)
- TMUXEnvironment(tmuxDependencies.DesktopEnvironment):refreshWindow()
- end,
- moveToProgrammingTab = function(self)
- os.execute("sleep 0.01")
- os.execute("xdotool key alt+m j")
- end,
- }
- end
- function TMUXEnvironment(desktopEnvironment)
- return {
- refreshWindow = function(self)
- self:openTMUXCommandLine()
- self:enterRespawnCommandForTmux()
- self:executeTMUXRespawnCommand()
- end,
- openTMUXCommandLine = function(self)
- os.execute("sleep 0.01")
- os.execute("xdotool key alt+m")
- os.execute("sleep 0.01")
- os.execute("xdotool key colon")
- os.execute("sleep 0.01")
- end,
- enterRespawnCommandForTmux = function(self)
- desktopEnvironment:execute({ "copy", "respawn-window -k" })
- desktopEnvironment:execute({ "paste" })
- os.execute("sleep 0.01")
- end,
- executeTMUXRespawnCommand = function(self)
- os.execute("xdotool key Return")
- os.execute("sleep 0.01")
- end,
- }
- end
- local fileFromNeovimBuffer = "~/activeTestFile.lua"
- local testCommandDependencies = { TypeOfFile(fileFromNeovimBuffer), AvailableTestingFrameworks() }
- local testCommand = TestCommand(testCommandDependencies)
- local tabKey = TabKey()
- local desktopEnvironment = X11(tabKey)
- local tmuxDependencies = { DesktopEnvironment = desktopEnvironment, Terminal = Terminal(desktopEnvironment) }
- TMUX(tmuxDependencies):executeTestCommandInTestingTabGiven(testCommand)
- os.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment