|
| 1 | +////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// _ _ ____ _ _ ___ ____ // |
| 3 | +// |_/ |__| |\ | | \ | | This file belongs to Kando, the cross-platform // |
| 4 | +// | \_ | | | \| |__/ |__| pie menu. Read more on github.com/kando-menu/kando // |
| 5 | +// // |
| 6 | +////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | + |
| 8 | +// SPDX-FileCopyrightText: ChandraChaan <[email protected]> |
| 9 | +// SPDX-License-Identifier: MIT |
| 10 | + |
| 11 | +import { expect } from 'chai'; |
| 12 | +import fs from 'fs'; |
| 13 | +import os from 'os'; |
| 14 | +import path from 'path'; |
| 15 | + |
| 16 | +import { OpenFileAction } from '../../src/common'; |
| 17 | +import { execute } from '../../src/main/actions/open-file'; |
| 18 | + |
| 19 | +describe('Open File action', function () { |
| 20 | + let tempDir: string; |
| 21 | + let originalPath: string | undefined; |
| 22 | + let argFile: string; |
| 23 | + |
| 24 | + beforeEach(function () { |
| 25 | + if (process.platform !== 'linux') { |
| 26 | + this.skip(); |
| 27 | + } |
| 28 | + |
| 29 | + // On Linux, the action runs `xdg-open` through a shell. We put a fake `xdg-open` at |
| 30 | + // the front of the PATH which records the first argument it receives. |
| 31 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kando-open-file-')); |
| 32 | + argFile = path.join(tempDir, 'received-arg'); |
| 33 | + |
| 34 | + const script = path.join(tempDir, 'xdg-open'); |
| 35 | + fs.writeFileSync(script, '#!/bin/sh\nprintf "%s" "$1" > "$KANDO_TEST_ARG_FILE"\n'); |
| 36 | + fs.chmodSync(script, 0o755); |
| 37 | + |
| 38 | + originalPath = process.env.PATH; |
| 39 | + process.env.PATH = `${tempDir}${path.delimiter}${originalPath}`; |
| 40 | + process.env.KANDO_TEST_ARG_FILE = argFile; |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(function () { |
| 44 | + if (tempDir) { |
| 45 | + process.env.PATH = originalPath; |
| 46 | + delete process.env.KANDO_TEST_ARG_FILE; |
| 47 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + const paths = [ |
| 52 | + '/home/user/Documents/plain.txt', |
| 53 | + '/home/user/My Documents/with spaces.txt', |
| 54 | + '/home/user/$HOME-notes.txt', |
| 55 | + '/home/user/Budget "2026".pdf', |
| 56 | + "/home/user/it's here.txt", |
| 57 | + '/home/user/back`tick`.txt', |
| 58 | + '/home/user/back\\slash.txt', |
| 59 | + ]; |
| 60 | + |
| 61 | + for (const filePath of paths) { |
| 62 | + it(`should pass the path unchanged to xdg-open: ${filePath}`, async function () { |
| 63 | + const action: OpenFileAction = { type: 'open-file', path: filePath }; |
| 64 | + |
| 65 | + await execute(action); |
| 66 | + |
| 67 | + expect(fs.readFileSync(argFile, 'utf8')).to.equal(filePath); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + it('should not run commands embedded in the path', async function () { |
| 72 | + const marker = path.join(tempDir, 'marker'); |
| 73 | + const action: OpenFileAction = { |
| 74 | + type: 'open-file', |
| 75 | + path: `/home/user/$(touch ${marker}).txt`, |
| 76 | + }; |
| 77 | + |
| 78 | + await execute(action); |
| 79 | + |
| 80 | + expect(fs.existsSync(marker)).to.equal(false); |
| 81 | + expect(fs.readFileSync(argFile, 'utf8')).to.equal(action.path); |
| 82 | + }); |
| 83 | +}); |
0 commit comments