Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lua/shelltime/utils/system.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,16 @@ function M.get_project_root(file_path)
return vim.fn.fnamemodify(file_path, ':p:h')
end

--- Get project name from root path
--- Get project name from root path (last 2 folder layers)
---@param project_root string Project root path
---@return string Project name
function M.get_project_name(project_root)
return vim.fn.fnamemodify(project_root, ':t')
local tail = vim.fn.fnamemodify(project_root, ':t')
local parent = vim.fn.fnamemodify(project_root, ':h:t')
if parent and parent ~= '' and parent ~= '/' then
return parent .. '/' .. tail
end
return tail
end

--- Generate UUID v4
Expand Down
8 changes: 5 additions & 3 deletions tests/system_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ describe('shelltime.utils.system', function()
end)

describe('get_project_name', function()
it('should extract directory name from path', function()
it('should extract last 2 folder layers from path', function()
local name = system.get_project_name('/home/user/projects/myapp')
assert.equals('myapp', name)
assert.equals('projects/myapp', name)
end)

it('should handle simple path', function()
it('should handle simple path with only 1 layer', function()
local name = system.get_project_name('/myapp')
assert.equals('myapp', name)
end)
Expand All @@ -196,6 +196,8 @@ describe('shelltime.utils.system', function()
local name = system.get_project_name(project_dir)
assert.is_string(name)
assert.is_true(#name > 0)
-- Should contain a slash for 2 layers
assert.is_true(name:find('/') ~= nil)
Comment on lines +199 to +200
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This assertion makes the test brittle because it depends on the environment where the tests are run. For example, if the project is located in a root directory like /my-project, get_project_name will correctly return my-project, but this assertion will fail because the name does not contain a /.

Test cases should be deterministic. I recommend removing this check and refactoring the parent test it('should handle current project', ...) to use hardcoded paths that cover specific scenarios. For example, you could add new tests for paths like /foo/bar and the root path /.

end)
end)
end)