From 5ce7ba260f54ef49b8a17c7a605b42f20d79abdb Mon Sep 17 00:00:00 2001 From: Kiran Patel <7103956+kiran94@users.noreply.github.com> Date: Tue, 18 Oct 2022 18:53:56 +0100 Subject: [PATCH] fix(output): ensure that current buffer is respected (#9) * fix(output): ensure buffer location is respected * chore(make): add test command * chore(ci): disabled stlua * chore(ci): add test --- .github/workflows/main.yml | 78 +++++++++++++++++++++++++++++--------- lua/maim/init.lua | 5 +++ lua/maim/utils.lua | 26 +++++++++++++ lua/tests/setup.vim | 1 + lua/tests/utils_spec.lua | 29 ++++++++++++++ makefile | 3 ++ 6 files changed, 124 insertions(+), 18 deletions(-) create mode 100644 lua/maim/utils.lua create mode 100644 lua/tests/setup.vim create mode 100644 lua/tests/utils_spec.lua diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b302efc..db16965 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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/stylua-action@1.0.0 - 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 }} diff --git a/lua/maim/init.lua b/lua/maim/init.lua index 3838777..7f09a28 100644 --- a/lua/maim/init.lua +++ b/lua/maim/init.lua @@ -1,3 +1,4 @@ +local utils = require("maim.utils") local M = {} M.setup = function() @@ -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') diff --git a/lua/maim/utils.lua b/lua/maim/utils.lua new file mode 100644 index 0000000..3df3e29 --- /dev/null +++ b/lua/maim/utils.lua @@ -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 diff --git a/lua/tests/setup.vim b/lua/tests/setup.vim new file mode 100644 index 0000000..959968c --- /dev/null +++ b/lua/tests/setup.vim @@ -0,0 +1 @@ +set rtp+=. diff --git a/lua/tests/utils_spec.lua b/lua/tests/utils_spec.lua new file mode 100644 index 0000000..c717eb8 --- /dev/null +++ b/lua/tests/utils_spec.lua @@ -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) diff --git a/makefile b/makefile index ebdff69..3b2f231 100644 --- a/makefile +++ b/makefile @@ -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' }"