Skip to content

Commit

Permalink
fix(output): ensure that current buffer is respected (#9)
Browse files Browse the repository at this point in the history
* fix(output): ensure buffer location is respected

* chore(make): add test command

* chore(ci): disabled stlua

* chore(ci): add test
  • Loading branch information
kiran94 authored Oct 18, 2022
1 parent cc042e4 commit 5ce7ba2
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 18 deletions.
78 changes: 60 additions & 18 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,71 @@
name: main

on:
push:
branches:
- master
- main
pull_request:


jobs:
run:
lint:
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- name: Checkout
uses: actions/checkout@v2

- uses: JohnnyMorganz/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --check .

- name: Release
uses: go-semantic-release/action@v1
if: github.ref == 'refs/heads/main'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout
uses: actions/checkout@v2
- uses: JohnnyMorganz/stylua-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --check .
test:
runs-on: ${{ matrix.os }}
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
include:
- id: Neovim Nightly
os: ubuntu-20.04
url: https://github.com/neovim/neovim/releases/download/nightly/nvim-linux64.tar.gz
manager: sudo apt-get
packages: -y ripgrep
- id: Neovim 0.8
os: ubuntu-20.04
url: https://github.com/neovim/neovim/releases/download/v0.8.0/nvim-linux64.tar.gz
manager: sudo apt-get
packages: -y ripgrep
steps:
- uses: actions/checkout@v2
- run: date +%F > todays-date
- name: Restore from todays cache
uses: actions/cache@v2
with:
path: _neovim
key: ${{ runner.os }}-${{ matrix.url }}-${{ hashFiles('todays-date') }}
- name: Prepare
run: |
${{ matrix.manager }} update
${{ matrix.manager }} install ${{ matrix.packages }}
test -d _neovim || {
mkdir -p _neovim
curl -sL ${{ matrix.url }} | tar xzf - --strip-components=1 -C "${PWD}/_neovim"
}
mkdir -p ~/.local/share/nvim/site/pack/vendor/start
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
git clone --depth 1 https://github.com/kyazdani42/nvim-web-devicons ~/.local/share/nvim/site/pack/vendor/start/nvim-web-devicons
ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start
- name: Run tests
run: |
export PATH="${PWD}/_neovim/bin:${PATH}"
export VIM="${PWD}/_neovim/share/nvim/runtime"
nvim --version
make test
release:
runs-on: ubuntu-latest
timeout-minutes: 10
needs: [lint, test]
steps:
- name: Release
uses: go-semantic-release/action@v1
if: github.ref == 'refs/heads/main'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions lua/maim/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require("maim.utils")
local M = {}

M.setup = function()
Expand All @@ -15,6 +16,10 @@ M.invoke_screenshot = function(output_path)
local executable = "maim"
local file_options = "-s"

local current_buffer = vim.api.nvim_buf_get_name(0)
local current_directory = vim.fs.dirname(current_buffer)
output_path = utils.get_current_buffer_directory(output_path, current_buffer, current_directory)

local command = executable .. " " .. file_options .. " " .. output_path
-- vim.notify(command, 'debug')

Expand Down
26 changes: 26 additions & 0 deletions lua/maim/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local M = {}

-- Produces a fully qualified path for the provided output path
-- Ensures that the current directory is respected
-- e.g if the user is within a repo:
-- README.md
-- src/
-- my_folder/
-- document.md
-- res/
-- my_other_folder/
-- document2.md
-- res/
-- If they run the plugin whilst inside 'my_folder' with output res/my_image.png
-- then the output image should go into src/my_folder/res/my_image.png
-- Some environments will interpret "res/my_image.png" as starting from the root repo which is usually
-- not what we want
M.get_current_buffer_directory = function(output_path, current_buffer, buffer_directory)
if output_path:sub(1, 1) == "/" then
return buffer_directory .. output_path
else
return buffer_directory .. "/" .. output_path
end
end

return M
1 change: 1 addition & 0 deletions lua/tests/setup.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set rtp+=.
29 changes: 29 additions & 0 deletions lua/tests/utils_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local mock = require("luassert.mock")

describe("get_current_buffer_directory", function()
it("respects output path /", function()
local utils = require("maim.utils")

local output_path = "/res/image.png"
local current_buffer = "/home/hello/document/projects/my_project/subfolder/document.md"
local current_directory = "/home/hello/document/projects/my_project/subfolder"
local expected = "/home/hello/document/projects/my_project/subfolder/res/image.png"

local output = utils.get_current_buffer_directory(output_path, current_buffer, current_directory)

assert.are.same(expected, output)
end)

it("respects output path", function()
local utils = require("maim.utils")

local output_path = "res/image.png"
local current_buffer = "/home/hello/document/projects/my_project/subfolder/document.md"
local current_directory = "/home/hello/document/projects/my_project/subfolder"
local expected = "/home/hello/document/projects/my_project/subfolder/res/image.png"

local output = utils.get_current_buffer_directory(output_path, current_buffer, current_directory)

assert.are.same(expected, output)
end)
end)
3 changes: 3 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ run:

help:
nvim --cmd "set rtp+=./" --cmd 'lua require("maim").setup()' --cmd 'h maim'

test:
nvim --cmd "set rtp+=./" --headless -c "PlenaryBustedDirectory lua/tests/ { minimal_init = 'lua/tests/setup.vim' }"

0 comments on commit 5ce7ba2

Please sign in to comment.