Skip to content

Commit

Permalink
use exec method for direnv
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 28, 2023
1 parent cfa2ca6 commit 71c6efa
Show file tree
Hide file tree
Showing 57 changed files with 293 additions and 198 deletions.
22 changes: 15 additions & 7 deletions .github/workflows/rtx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ jobs:
unit:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v3
- name: Rust Cache
uses: Swatinem/rust-cache@v2
- name: Install direnv
run: sudo apt-get update; sudo apt-get install direnv
- name: Install just
uses: taiki-e/install-action@just
- name: just test-unit
- name: Run just test-unit
uses: nick-fields/retry@v2
env:
GITHUB_API_TOKEN: ${{ secrets.RTX_GITHUB_BOT_TOKEN }}
Expand All @@ -33,17 +36,19 @@ jobs:

coverage:
runs-on: ubuntu-latest
#if: false
container:
image: xd009642/tarpaulin:develop-nightly
options: --security-opt seccomp=unconfined
steps:
- uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v3
- name: Rust Cache
uses: Swatinem/rust-cache@v2
- name: Install direnv
run: curl -sfL https://direnv.net/install.sh | bash
- name: Install just
uses: taiki-e/install-action@just
- name: Generate code coverage
- name: Run tests with coverage
uses: nick-fields/retry@v2
env:
GITHUB_API_TOKEN: ${{ secrets.RTX_GITHUB_BOT_TOKEN }}
Expand Down Expand Up @@ -121,10 +126,13 @@ jobs:
e2e-linux:
runs-on: ubuntu-22.04
needs: [ build-linux ]
container:
image: xd009642/tarpaulin:develop-nightly
options: --security-opt seccomp=unconfined
steps:
- uses: actions/checkout@v3
- name: Install zsh/fish
run: sudo apt-get update; sudo apt-get install zsh fish
- name: Install zsh/fish/direnv
run: sudo apt-get update; sudo apt-get install zsh fish direnv
- uses: actions/download-artifact@v3
with:
name: tarball-x86_64-unknown-linux-gnu
Expand Down
1 change: 0 additions & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
shellcheck 0.9.0
shfmt 3.6.0 # test comment
#nodejs 18.13.0
nodejs system
jq latest
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ toml_edit = "0.18.0"
url = "2.3.1"
versions = "4.1.0"
build-time = "0.1.2"
serde_json = "1.0.91"

[target.'cfg(unix)'.dependencies]
exec = "0.3.1"
Expand Down
1 change: 1 addition & 0 deletions e2e/run_all_tests
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -euo pipefail
export RTX_MISSING_RUNTIME_BEHAVIOR="autoinstall"
export RTX_DATA_DIR="/tmp/rtx"
export PATH="$PWD/target/debug:$PATH"
unset GOPATH

rtx plugin add nodejs
rtx plugin add shfmt
Expand Down
2 changes: 0 additions & 2 deletions e2e/test_local
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ assert "rtx local -p" "#python 3.11.1 3.10.9 # foo
shellcheck 0.9.0
shfmt 3.6.0 # test comment
#nodejs 18.13.0
nodejs system
jq latest
"

assert "rtx local -p [email protected]" "#python 3.11.1 3.10.9 # foo
shellcheck 0.9.0
shfmt 3.5.0 # test comment
#nodejs 18.13.0
nodejs system
jq latest
"

Expand Down
12 changes: 9 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ alias t := test
test: test-unit test-e2e

test-setup: build
rtx install

test-update-snapshots: test-setup
cargo insta test --accept
Expand All @@ -28,11 +27,18 @@ test-unit: test-setup
test-e2e: test-setup build
./e2e/run_all_tests

test-coverage: test-setup
rtx --version
test-coverage: clean test-setup
cargo +nightly tarpaulin \
--all-features --workspace \
--timeout 120 --out Xml --ignore-tests
# RUSTFLAGS='-Cinstrument-coverage' LLVM_PROFILE_FILE='cargo-test-%p-%m.profraw' cargo test
# grcov . --binary-path ./target/debug/ -s . -t lcov --branch --ignore-not-existing --ignore "/*" -o lcov.info

clean:
cargo clean
rm -rf target
rm -rf *.profraw
rm -rf coverage

lint:
cargo clippy
Expand Down
6 changes: 2 additions & 4 deletions src/cli/activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ mod test {

use crate::assert_cli;

use super::*;

#[test]
fn test_activate_zsh() {
let Output { stdout, .. } = assert_cli!("activate", "-s", "zsh");
assert_display_snapshot!(stdout.content);
let stdout = assert_cli!("activate", "-s", "zsh");
assert_display_snapshot!(stdout);
}
}
5 changes: 2 additions & 3 deletions src/cli/alias/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ const AFTER_LONG_HELP: &str = indoc! {r#"
mod test {
use crate::assert_cli;
use crate::cli::test::ensure_plugin_installed;
use crate::output::Output;

#[test]
fn test_alias_ls() {
ensure_plugin_installed("shfmt");
let Output { stdout, .. } = assert_cli!("aliases");
assert!(stdout.content.contains("my/alias"));
let stdout = assert_cli!("aliases");
assert!(stdout.contains("my/alias"));
}
}
10 changes: 4 additions & 6 deletions src/cli/asdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,17 @@ fn list_versions(out: &mut Output, args: &Vec<String>) -> Result<()> {
mod test {
use crate::assert_cli;

use super::*;

#[test]
fn test_fake_asdf() {
let Output { stdout, .. } = assert_cli!("asdf", "-v");
assert!(stdout.content.starts_with("rtx "));
let stdout = assert_cli!("asdf", "-v");
assert!(stdout.starts_with("rtx "));
}

#[test]
fn test_fake_asdf_list() {
assert_cli!("plugin", "install", "shfmt");
assert_cli!("install", "shfmt@2");
let Output { stdout, .. } = assert_cli!("asdf", "list", "shfmt");
assert!(stdout.content.contains('2'));
let stdout = assert_cli!("asdf", "list", "shfmt");
assert!(stdout.contains('2'));
}
}
10 changes: 4 additions & 6 deletions src/cli/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,21 @@ mod test {

use crate::assert_cli;

use super::*;

#[test]
fn test_current() {
assert_cli!("plugin", "add", "shellcheck");
assert_cli!("install");
let Output { stdout, .. } = assert_cli!("current");
let stdout = assert_cli!("current");
let re = Regex::new(r"-> shellcheck\s+0\.9\.0\s+").unwrap();
assert!(re.is_match(&stdout.content));
assert!(re.is_match(&stdout));
}

#[test]
fn test_current_with_runtimes() {
assert_cli!("plugin", "add", "shfmt");
assert_cli!("install");
let Output { stdout, .. } = assert_cli!("current", "shfmt");
let stdout = assert_cli!("current", "shfmt");
let re = Regex::new(r"-> shfmt\s+3\.5\.2\s+").unwrap();
assert!(re.is_match(&stdout.content));
assert!(re.is_match(&stdout));
}
}
6 changes: 2 additions & 4 deletions src/cli/deactivate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ mod test {

use crate::assert_cli;

use super::*;

#[test]
fn test_deactivate_zsh() {
std::env::set_var("NO_COLOR", "1");
let Output { stdout, .. } = assert_cli!("deactivate", "-s", "zsh");
assert_display_snapshot!(stdout.content);
let stdout = assert_cli!("deactivate", "-s", "zsh");
assert_display_snapshot!(stdout);
}
}
3 changes: 2 additions & 1 deletion src/cli/direnv/activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ impl Command for DirenvActivate {
fn run(self, _config: Config, out: &mut Output) -> Result<()> {
rtxprintln!(
out,
// source_env "$(rtx direnv envrc "$@")"
indoc! {r#"
### Do not edit. This was autogenerated by 'rtx direnv' ###
use_rtx() {{
source_env "$(rtx direnv envrc "$@")"
direnv_load rtx direnv exec
}}
"#}
);
Expand Down
13 changes: 7 additions & 6 deletions src/cli/direnv/envrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::ops::Deref;
use color_eyre::eyre::Result;

use crate::cli::command::Command;
use crate::config::settings::MissingRuntimeBehavior::{AutoInstall, Warn};
use crate::config::settings::MissingRuntimeBehavior::{Prompt, Warn};
use crate::config::Config;
use crate::dirs;
use crate::hash::hash_to_str;
use crate::output::Output;
use crate::{dirs, env};

/// [internal] This is an internal command that writes an envrc file
/// for direnv to consume.
Expand All @@ -19,11 +19,11 @@ pub struct Envrc {}

impl Command for Envrc {
fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
if config.settings.missing_runtime_behavior == AutoInstall {
if config.settings.missing_runtime_behavior == Prompt {
config.settings.missing_runtime_behavior = Warn;
}
config.ensure_installed()?;
let envrc_path = dirs::ROOT
let envrc_path = env::RTX_TMP_DIR
.join("direnv")
.join(hash_to_str(dirs::CURRENT.deref()) + ".envrc");

Expand Down Expand Up @@ -67,8 +67,9 @@ mod test {

#[test]
fn test_direnv_envrc() {
let Output { stdout, .. } = assert_cli!("direnv", "envrc");
let envrc = fs::read_to_string(stdout.content.trim()).unwrap();
assert_cli!("install");
let stdout = assert_cli!("direnv", "envrc");
let envrc = fs::read_to_string(stdout.trim()).unwrap();
let envrc = envrc.replace(dirs::HOME.to_string_lossy().as_ref(), "~");
assert_display_snapshot!(envrc);
}
Expand Down
59 changes: 59 additions & 0 deletions src/cli/direnv/exec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use color_eyre::eyre::Result;
use serde_derive::Deserialize;

use crate::cli::command::Command;
use crate::cmd;
use crate::config::settings::MissingRuntimeBehavior::{Prompt, Warn};
use crate::config::Config;
use crate::output::Output;

/// [internal] This is an internal command that writes an envrc file
/// for direnv to consume.
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, hide = true)]
pub struct DirenvExec {}

#[derive(Debug, Default, Deserialize)]
struct DirenvWatches {
#[serde(rename(deserialize = "DIRENV_WATCHES"))]
watches: String,
}

impl Command for DirenvExec {
fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
if config.settings.missing_runtime_behavior == Prompt {
config.settings.missing_runtime_behavior = Warn;
}
config.ensure_installed()?;
let mut cmd = if cfg!(test) {
cmd!("env")
} else {
cmd!("direnv", "dump")
};

for (k, v) in config.env()? {
cmd = cmd.env(k, v);
}
cmd = cmd.env("PATH", config.path_env()?);

let json = cmd!("direnv", "watch", "json", ".tool-versions").read()?;
let w: DirenvWatches = serde_json::from_str(&json)?;
cmd = cmd.env("DIRENV_WATCHES", w.watches);

rtxprint!(out, "{}", cmd.read()?);
Ok(())
}
}

#[cfg(test)]
mod test {
use crate::assert_cli;
use crate::cli::test::grep;
use pretty_assertions::assert_str_eq;

#[test]
fn test_direnv_exec() {
let stdout = assert_cli!("direnv", "exec");
assert_str_eq!(grep(stdout, "JDXCODE_TINY="), "JDXCODE_TINY=2.1.0");
}
}
3 changes: 3 additions & 0 deletions src/cli/direnv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::output::Output;

mod activate;
mod envrc;
mod exec;

/// Output direnv function to use rtx inside direnv
///
Expand All @@ -25,6 +26,7 @@ pub struct Direnv {
#[derive(Debug, Subcommand)]
enum Commands {
Envrc(envrc::Envrc),
Exec(exec::DirenvExec),
Activate(activate::DirenvActivate),
}

Expand All @@ -33,6 +35,7 @@ impl Commands {
match self {
Self::Activate(cmd) => cmd.run(config, out),
Self::Envrc(cmd) => cmd.run(config, out),
Self::Exec(cmd) => cmd.run(config, out),
}
}
}
Expand Down
Loading

0 comments on commit 71c6efa

Please sign in to comment.