Skip to content

Commit

Permalink
feat: add xonsh support & add submit command (#22)
Browse files Browse the repository at this point in the history
* feat: add xonsh support

Signed-off-by: Chapman Pendery <[email protected]>

* fix: drop autocomplete from ci

Signed-off-by: Chapman Pendery <[email protected]>

* fix: xonsh version detection and submit on empty

Signed-off-by: Chapman Pendery <[email protected]>

* fix: make examples use submit

Signed-off-by: Chapman Pendery <[email protected]>

---------

Signed-off-by: Chapman Pendery <[email protected]>
  • Loading branch information
cpendery committed Mar 9, 2024
1 parent 08d2c71 commit c2594ce
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 50 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ jobs:
if: github.event_name == 'pull_request'
run: npx commitlint --from ${{ github.event.pull_request.base.sha }} --verbose

- name: setup macOS shells
if: matrix.os == 'macos-latest'
shell: bash
run: |
brew install fish
brew install zsh
sudo chmod g-w /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/share/zsh
- name: setup linux shells
if: matrix.os == 'ubuntu-latest'
shell: bash
run: |
sudo apt-add-repository ppa:fish-shell/release-3
sudo apt-get update
sudo apt install fish zsh
sudo chmod -R 755 /usr/share/zsh/vendor-completions
sudo chown -R root:root /usr/share/zsh/vendor-completions
sudo chmod -R 755 /usr/share/zsh
sudo chown -R root:root /usr/share/zsh
- name: setup windows shells
if: matrix.os == 'windows-latest'
shell: pwsh
run: |
python -m pip install 'xonsh[full]'
- name: Run npm run lint
run: npm run lint

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ npx @microsoft/tui-test

### Multi-platform / Multi-shell • No more "it works in my shell"

**Multi-platform**. TUI Test supports testing on macOS, Linux, and Windows with a wide range of shells when installed: `cmd`, `windows powershell`, `powershell`, `bash`, `git-bash`, `fish`, and `zsh`.
**Multi-platform**. TUI Test supports testing on macOS, Linux, and Windows with a wide range of shells when installed: `cmd`, `windows powershell`, `powershell`, `bash`, `git-bash`, `fish`, `zsh`, and `xonsh`.

**Wide-support**. TUI Test uses [xterm.js](https://xtermjs.org/) to render the terminal, which is a widely used terminal emulator in projects like [VSCode](https://github.com/microsoft/vscode) and [Hyper](https://github.com/vercel/hyper).

Expand Down Expand Up @@ -96,7 +96,7 @@ This code snippet shows how to use rich assertions of the terminal.
import { test, expect } from "@microsoft/tui-test";

test("make a regex assertion", async ({ terminal }) => {
terminal.write("ls -l\r")
terminal.submit("ls -l")

await expect(terminal.getByText(/total [0-9]{3}/g)).toBeVisible();
});
Expand Down
8 changes: 4 additions & 4 deletions examples/assertions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import { test, expect, Shell } from "@microsoft/tui-test";
test.use({ shell: Shell.Powershell });

test("make a regex assertion", async ({ terminal }) => {
terminal.write("dir | measure\r");
terminal.submit("dir | measure");

await expect(terminal.getByText(/Count\s*:\s*[0-9]{2}/g)).toBeVisible();
});

test("make a text assertion", async ({ terminal }) => {
terminal.write("echo 1\r");
terminal.submit("echo 1");

await expect(terminal.getByText("1")).toBeVisible();
});

test("make a negative text assertion", async ({ terminal }) => {
terminal.write("echo 1\r");
terminal.submit("echo 1");
await expect(terminal.getByText("1")).toBeVisible();

terminal.write("clear\r");
terminal.submit("clear");
await expect(terminal.getByText("clear")).not.toBeVisible();
});
1 change: 1 addition & 0 deletions shell/shellIntegration.xsh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$PROMPT = "> "
14 changes: 13 additions & 1 deletion src/reporter/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const loadShellVersions = async (
}
break;
}
case (Shell.WindowsPowershell, Shell.Powershell): {
case Shell.Powershell:
case Shell.WindowsPowershell: {
const output = (
await execAsync("$PSVersionTable", { shell: target })
).stdout;
Expand Down Expand Up @@ -67,6 +68,17 @@ export const loadShellVersions = async (
}
break;
}
case Shell.Xonsh: {
const output = (
await execAsync(`${target} -m xonsh -V`)
).stdout.trim();
// eslint-disable-next-line no-useless-escape
const match = output.match(/^xonsh\/([0-9\.]*)$/)?.at(1);
if (match != null) {
version = match;
}
break;
}
}
} catch {
/* empty */
Expand Down
31 changes: 30 additions & 1 deletion src/terminal/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export enum Shell {
Cmd = "cmd",
Fish = "fish",
Zsh = "zsh",
Xonsh = "xonsh",
}

export const defaultShell =
Expand All @@ -29,7 +30,7 @@ export const zdotdir = path.join(os.tmpdir(), `tui-test-zsh`);

export const shellLaunch = async (shell: Shell) => {
const platform = os.platform();
const shellTarget =
let shellTarget =
shell == Shell.Bash && platform == "win32"
? await gitBashPath()
: platform == "win32"
Expand Down Expand Up @@ -71,6 +72,30 @@ export const shellLaunch = async (shell: Shell) => {
`. ${path.join(shellFolderPath, "shellIntegration.fish").replace(/(\s+)/g, "\\$1")}`,
];
break;
case Shell.Xonsh: {
const sharedConfig =
os.platform() == "win32"
? path.join("C:\\ProgramData", "xonsh", "xonshrc")
: path.join("etc", "xonsh", "xonshrc");
const userConfigs = [
path.join(os.homedir(), ".xonshrc"),
path.join(os.homedir(), ".config", "xonsh", "rc.xsh"),
path.join(os.homedir(), ".config", "xonsh", "rc.d"),
];
const configs = [sharedConfig, ...userConfigs].filter((config) =>
fs.existsSync(config)
);

shellArgs = [
"-m",
"xonsh",
"--rc",
...configs,
path.join(shellFolderPath, "shellIntegration.xsh"),
];
shellTarget = await getPythonPath();
break;
}
}

return { shellTarget, shellArgs };
Expand Down Expand Up @@ -116,6 +141,10 @@ export const setupZshDotfiles = async () => {
);
};

export const getPythonPath = async (): Promise<string> => {
return await which("python", { nothrow: true });
};

const gitBashPath = async (): Promise<string> => {
const gitBashPaths = await getGitBashPaths();
for (const gitBashPath of gitBashPaths) {
Expand Down
Loading

0 comments on commit c2594ce

Please sign in to comment.