Skip to content

Commit

Permalink
Print activation instructions for a venv after one has been created (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
0v00 authored Feb 20, 2024
1 parent d05cb84 commit 7741e1c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
17 changes: 17 additions & 0 deletions crates/uv/src/commands/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,22 @@ async fn venv_impl(
}
}

if cfg!(windows) {
writeln!(
printer,
// This should work whether the user is on CMD or PowerShell:
"Activate with: {}\\Scripts\\activate",
path.normalized_display().cyan()
)
.into_diagnostic()?;
} else {
writeln!(
printer,
"Activate with: source {}/bin/activate",
path.normalized_display().cyan()
)
.into_diagnostic()?;
};

Ok(ExitStatus::Success)
}
44 changes: 44 additions & 0 deletions crates/uv/tests/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ fn create_venv() -> Result<()> {

// Create a virtual environment at `.venv`.
let filter_venv = regex::escape(&venv.normalized_display().to_string());
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at .+",
"Using Python [VERSION] interpreter at [PATH]",
),
(&filter_venv, "/home/ferris/project/.venv"),
(
&filter_prompt,
"Activate with: source /home/ferris/project/.venv/bin/activate",
),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
Expand All @@ -45,19 +50,25 @@ fn create_venv() -> Result<()> {
----- stderr -----
Using Python [VERSION] interpreter at [PATH]
Creating virtualenv at: /home/ferris/project/.venv
Activate with: source /home/ferris/project/.venv/bin/activate
"###
);

venv.assert(predicates::path::is_dir());

// Create a virtual environment at the same location, which should replace it.
let filter_venv = regex::escape(&venv.normalized_display().to_string());
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at .+",
"Using Python [VERSION] interpreter at [PATH]",
),
(&filter_venv, "/home/ferris/project/.venv"),
(
&filter_prompt,
"Activate with: source /home/ferris/project/.venv/bin/activate",
),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
Expand All @@ -78,6 +89,7 @@ fn create_venv() -> Result<()> {
----- stderr -----
Using Python [VERSION] interpreter at [PATH]
Creating virtualenv at: /home/ferris/project/.venv
Activate with: source /home/ferris/project/.venv/bin/activate
"###
);

Expand All @@ -94,12 +106,14 @@ fn create_venv_defaults_to_cwd() -> Result<()> {
let venv = temp_dir.child(".venv");

let filter_venv = regex::escape(&venv.normalized_display().to_string());
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at .+",
"Using Python [VERSION] interpreter at [PATH]",
),
(&filter_venv, "/home/ferris/project/.venv"),
(&filter_prompt, "Activate with: source .venv/bin/activate"),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
Expand All @@ -119,6 +133,7 @@ fn create_venv_defaults_to_cwd() -> Result<()> {
----- stderr -----
Using Python [VERSION] interpreter at [PATH]
Creating virtualenv at: .venv
Activate with: source .venv/bin/activate
"###
);

Expand All @@ -135,12 +150,17 @@ fn seed() -> Result<()> {
let venv = temp_dir.child(".venv");

let filter_venv = regex::escape(&venv.normalized_display().to_string());
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at .+",
"Using Python [VERSION] interpreter at [PATH]",
),
(&filter_venv, "/home/ferris/project/.venv"),
(
&filter_prompt,
"Activate with: source /home/ferris/project/.venv/bin/activate",
),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
Expand All @@ -163,6 +183,7 @@ fn seed() -> Result<()> {
Using Python [VERSION] interpreter at [PATH]
Creating virtualenv at: /home/ferris/project/.venv
+ pip==23.3.1
Activate with: source /home/ferris/project/.venv/bin/activate
"###
);

Expand All @@ -179,12 +200,17 @@ fn seed_older_python_version() -> Result<()> {
let venv = temp_dir.child(".venv");

let filter_venv = regex::escape(&venv.normalized_display().to_string());
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at .+",
"Using Python [VERSION] interpreter at [PATH]",
),
(&filter_venv, "/home/ferris/project/.venv"),
(
&filter_prompt,
"Activate with: source /home/ferris/project/.venv/bin/activate",
),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
Expand All @@ -209,6 +235,7 @@ fn seed_older_python_version() -> Result<()> {
+ setuptools==68.2.2
+ pip==23.3.1
+ wheel==0.41.3
Activate with: source /home/ferris/project/.venv/bin/activate
"###
);

Expand Down Expand Up @@ -317,9 +344,14 @@ fn create_venv_python_patch() -> Result<()> {
let venv = temp_dir.child(".venv");

let filter_venv = regex::escape(&venv.normalized_display().to_string());
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
let filters = &[
(r"interpreter at .+", "interpreter at [PATH]"),
(&filter_venv, "/home/ferris/project/.venv"),
(
&filter_prompt,
"Activate with: source /home/ferris/project/.venv/bin/activate",
),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
Expand Down Expand Up @@ -406,12 +438,17 @@ fn empty_dir_exists() -> Result<()> {
venv.create_dir_all()?;

let filter_venv = regex::escape(&venv.normalized_display().to_string());
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at .+",
"Using Python [VERSION] interpreter at [PATH]",
),
(&filter_venv, "/home/ferris/project/.venv"),
(
&filter_prompt,
"Activate with: source /home/ferris/project/.venv/bin/activate",
),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
Expand All @@ -432,6 +469,7 @@ fn empty_dir_exists() -> Result<()> {
----- stderr -----
Using Python [VERSION] interpreter at [PATH]
Creating virtualenv at: /home/ferris/project/.venv
Activate with: source /home/ferris/project/.venv/bin/activate
"###
);

Expand Down Expand Up @@ -497,12 +535,17 @@ fn virtualenv_compatibility() -> Result<()> {

// Create a virtual environment at `.venv`, passing the redundant `--clear` flag.
let filter_venv = regex::escape(&venv.normalized_display().to_string());
let filter_prompt = r"Activate with: (?:.*)\\Scripts\\activate";
let filters = &[
(
r"Using Python 3\.\d+\.\d+ interpreter at .+",
"Using Python [VERSION] interpreter at [PATH]",
),
(&filter_venv, "/home/ferris/project/.venv"),
(
&filter_prompt,
"Activate with: source /home/ferris/project/.venv/bin/activate",
),
];
uv_snapshot!(filters, Command::new(get_bin())
.arg("venv")
Expand All @@ -524,6 +567,7 @@ fn virtualenv_compatibility() -> Result<()> {
warning: virtualenv's `--clear` has no effect (uv always clears the virtual environment).
Using Python [VERSION] interpreter at [PATH]
Creating virtualenv at: /home/ferris/project/.venv
Activate with: source /home/ferris/project/.venv/bin/activate
"###
);

Expand Down

0 comments on commit 7741e1c

Please sign in to comment.