Skip to content

Commit

Permalink
Add is_valid_channel and use it everywhere `version_db.available_cha…
Browse files Browse the repository at this point in the history
…nnels` is used
  • Loading branch information
christiangnrd committed Sep 3, 2024
1 parent f160605 commit 4aa5653
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/bin/julialauncher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use itertools::Itertools;
use juliaup::config_file::{load_config_db, JuliaupConfig, JuliaupConfigChannel};
use juliaup::global_paths::get_paths;
use juliaup::jsonstructs_versionsdb::JuliaupVersionDB;
use juliaup::operations::is_valid_channel;
use juliaup::versions_file::load_versions_db;
#[cfg(not(windows))]
use nix::{
Expand Down Expand Up @@ -178,21 +179,21 @@ fn get_julia_path_from_channel(
.get(channel)
.ok_or_else(|| match juliaup_channel_source {
JuliaupChannelSource::CmdLine => {
if versions_db.available_channels.contains_key(channel) {
if is_valid_channel(versions_db, &channel.to_string()) {
UserError { msg: format!("`{}` is not installed. Please run `juliaup add {}` to install channel or version.", channel, channel) }
} else {
UserError { msg: format!("ERROR: Invalid Juliaup channel `{}`. Please run `juliaup list` to get a list of valid channels and versions.", channel) }
}
}.into(),
JuliaupChannelSource::EnvVar=> {
if versions_db.available_channels.contains_key(channel) {
if is_valid_channel(versions_db, &channel.to_string()) {
UserError { msg: format!("`{}` for environment variable JULIAUP_CHANNEL is not installed. Please run `juliaup add {}` to install channel or version.", channel, channel) }
} else {
UserError { msg: format!("ERROR: Invalid Juliaup channel `{}` in environment variable JULIAUP_CHANNEL. Please run `juliaup list` to get a list of valid channels and versions.", channel) }
}
}.into(),
JuliaupChannelSource::Override=> {
if versions_db.available_channels.contains_key(channel) {
if is_valid_channel(versions_db, &channel.to_string()) {
UserError { msg: format!("`{}` for directory override is not installed. Please run `juliaup add {}` to install channel or version.", channel, channel) }
} else {
UserError { msg: format!("ERROR: Invalid Juliaup channel `{}` in directory override. Please run `juliaup list` to get a list of valid channels and versions.", channel) }
Expand Down
3 changes: 2 additions & 1 deletion src/command_default.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::operations::is_valid_channel;
use crate::versions_file::load_versions_db;
use crate::{config_file::*, global_paths::GlobalPaths};
use anyhow::{bail, Context, Result};
Expand All @@ -9,7 +10,7 @@ pub fn run_command_default(channel: &str, paths: &GlobalPaths) -> Result<()> {
if !config_file.data.installed_channels.contains_key(channel) {
let version_db = load_versions_db(paths)
.with_context(|| "`default` command failed to load versions db.")?;
if !version_db.available_channels.contains_key(channel) {
if !is_valid_channel(&version_db, &channel.to_string()) {
bail!("'{}' is not a valid Julia version.", channel);
} else {
bail!(
Expand Down
3 changes: 2 additions & 1 deletion src/command_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::config_file::{load_mut_config_db, save_config_db};
use crate::global_paths::GlobalPaths;
#[cfg(not(windows))]
use crate::operations::create_symlink;
use crate::operations::is_valid_channel;
use crate::versions_file::load_versions_db;
use anyhow::{bail, Context, Result};
use path_absolutize::Absolutize;
Expand All @@ -24,7 +25,7 @@ pub fn run_command_link(
bail!("Channel name `{}` is already used.", channel)
}

if versiondb_data.available_channels.contains_key(channel) {
if is_valid_channel(&versiondb_data, &channel.to_string()) {
eprintln!("WARNING: The channel name `{}` is also a system channel. By linking your custom binary to this channel you are hiding this system channel.", channel);
}

Expand Down
24 changes: 24 additions & 0 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,30 @@ pub fn compatible_archs() -> Result<Vec<String>> {
}
}

// which nightly channels are compatible with the current system
fn compatible_nightly_channels() -> Result<Vec<String>> {
let archs: Result<Vec<String>> = compatible_archs();

if archs.is_ok() {
let channels: Vec<String> = std::iter::once("nightly".to_string())
.chain(archs?.into_iter().map(|arch| format!("nightly~{}", arch)))
.collect();
Ok(channels)
} else {
archs
}
}

// considers the nightly channels as system channels
pub fn is_valid_channel(versions_db: &JuliaupVersionDB, channel: &String) -> bool {
let regular = versions_db.available_channels.contains_key(channel);

let nightly_chans = compatible_nightly_channels();

let nightly = nightly_chans.is_ok_and(|nightly_chans| nightly_chans.contains(channel));
regular || nightly
}

// Identify the unversioned name of a nightly (e.g., `latest-macos-x86_64`) for a channel
pub fn channel_to_name(channel: &String) -> Result<String> {
let mut parts = channel.splitn(2, '~');
Expand Down
26 changes: 26 additions & 0 deletions tests/channel_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,30 @@ fn channel_selection() {
.assert()
.failure()
.stderr("ERROR: Invalid Juliaup channel `1.8.6`. Please run `juliaup list` to get a list of valid channels and versions.\n");

// https://github.com/JuliaLang/juliaup/issues/766
Command::cargo_bin("julia")
.unwrap()
.arg("+1.8.2")
.arg("-e")
.arg("print(VERSION)")
.env("JULIA_DEPOT_PATH", depot_dir.path())
.env("JULIAUP_DEPOT_PATH", depot_dir.path())
.env("JULIAUP_CHANNEL", "1.7.4")
.assert()
.failure()
.stderr("`1.8.2` is not installed. Please run `juliaup add 1.8.2` to install channel or version.\n");

// https://github.com/JuliaLang/juliaup/issues/820
Command::cargo_bin("julia")
.unwrap()
.arg("+nightly")
.arg("-e")
.arg("print(VERSION)")
.env("JULIA_DEPOT_PATH", depot_dir.path())
.env("JULIAUP_DEPOT_PATH", depot_dir.path())
.env("JULIAUP_CHANNEL", "1.7.4")
.assert()
.failure()
.stderr("`nightly` is not installed. Please run `juliaup add nightly` to install channel or version.\n");
}

0 comments on commit 4aa5653

Please sign in to comment.