Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extension stripping, add tests, improve artifact module organization #40

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- Fixed artifact names with versions in them suchas `lune-0.8.6-linux-x86_64.zip` no longer installing correctly in Rokit `0.1.6` ([#40])

[#40]: https://github.com/rojo-rbx/rokit/pull/40

## `0.1.6` - July 15th, 2024

### Fixed
Expand Down
134 changes: 134 additions & 0 deletions lib/sources/artifact/format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use std::{fmt, str::FromStr};

use super::util::split_filename_and_extensions;

/**
An artifact format supported by Rokit.
*/
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArtifactFormat {
Zip,
Tar,
TarGz,
}

impl ArtifactFormat {
#[must_use]
pub fn as_str(&self) -> &'static str {
match self {
Self::Zip => "zip",
Self::Tar => "tar",
Self::TarGz => "tar.gz",
}
}

#[must_use]
pub fn from_extensions<'a>(extensions: impl AsRef<[&'a str]>) -> Option<Self> {
match extensions.as_ref() {
[.., ext] if ext.eq_ignore_ascii_case("zip") => Some(Self::Zip),
[.., ext] if ext.eq_ignore_ascii_case("tar") => Some(Self::Tar),
[.., ext] if ext.eq_ignore_ascii_case("tgz") => Some(Self::TarGz),
[.., ext1, ext2]
if ext1.eq_ignore_ascii_case("tar") && ext2.eq_ignore_ascii_case("gz") =>
{
Some(Self::TarGz)
}
_ => None,
}
}

#[must_use]
pub fn from_path_or_url(path_or_url: impl AsRef<str>) -> Option<Self> {
let path_or_url = path_or_url.as_ref();
let (_, extensions) = split_filename_and_extensions(path_or_url);
Self::from_extensions(extensions)
}
}

impl FromStr for ArtifactFormat {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let l = s.trim().to_lowercase();
match l.as_str() {
"zip" => Ok(Self::Zip),
"tar" => Ok(Self::Tar),
"tar.gz" | "tgz" => Ok(Self::TarGz),
_ => Err(format!("unknown artifact format '{l}'")),
}
}
}

impl fmt::Display for ArtifactFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(f)
}
}

#[cfg(test)]
mod tests {
use super::*;

fn format_from_str(s: &str) -> Option<ArtifactFormat> {
let (_, extensions) = split_filename_and_extensions(s);
ArtifactFormat::from_extensions(extensions)
}

#[test]
fn format_from_extensions_valid() {
assert_eq!(format_from_str("file.zip"), Some(ArtifactFormat::Zip));
assert_eq!(format_from_str("file.tar"), Some(ArtifactFormat::Tar));
assert_eq!(format_from_str("file.tar.gz"), Some(ArtifactFormat::TarGz));
assert_eq!(
format_from_str("file.with.many.extensions.tar.gz.zip"),
Some(ArtifactFormat::Zip)
);
assert_eq!(
format_from_str("file.with.many.extensions.zip.gz.tar"),
Some(ArtifactFormat::Tar)
);
assert_eq!(
format_from_str("file.with.many.extensions.tar.gz"),
Some(ArtifactFormat::TarGz)
);
}

#[test]
fn format_from_extensions_invalid() {
assert_eq!(format_from_str("file-name"), None);
assert_eq!(format_from_str("some/file.exe"), None);
assert_eq!(format_from_str("really.long.file.name"), None);
}

#[test]
fn format_from_real_tools() {
assert_eq!(
format_from_str("wally-v0.3.2-linux.zip"),
Some(ArtifactFormat::Zip)
);
assert_eq!(
format_from_str("lune-0.8.6-macos-aarch64.zip"),
Some(ArtifactFormat::Zip)
);
assert_eq!(
format_from_str("just-1.31.0-aarch64-apple-darwin.tar.gz"),
Some(ArtifactFormat::TarGz)
);
assert_eq!(
format_from_str("sentry-cli-linux-i686-2.32.1.tgz"),
Some(ArtifactFormat::TarGz)
);
}

#[test]
fn format_case_sensitivity() {
assert_eq!(format_from_str("file.ZIP"), Some(ArtifactFormat::Zip));
assert_eq!(format_from_str("file.zip"), Some(ArtifactFormat::Zip));
assert_eq!(format_from_str("file.Zip"), Some(ArtifactFormat::Zip));
assert_eq!(format_from_str("file.tar"), Some(ArtifactFormat::Tar));
assert_eq!(format_from_str("file.TAR"), Some(ArtifactFormat::Tar));
assert_eq!(format_from_str("file.Tar"), Some(ArtifactFormat::Tar));
assert_eq!(format_from_str("file.tar.gz"), Some(ArtifactFormat::TarGz));
assert_eq!(format_from_str("file.TAR.GZ"), Some(ArtifactFormat::TarGz));
assert_eq!(format_from_str("file.Tar.Gz"), Some(ArtifactFormat::TarGz));
}
}
181 changes: 6 additions & 175 deletions lib/sources/artifact.rs → lib/sources/artifact/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::{fmt, str::FromStr};

use tracing::instrument;
use url::Url;

use crate::{
descriptor::{Descriptor, OS},
result::RokitResult,
tool::ToolSpec,
util::path::split_filename_and_extensions,
};

use super::{
Expand All @@ -17,111 +14,14 @@ use super::{
ExtractError,
};

/**
An artifact provider supported by Rokit.

The default provider is [`ArtifactProvider::GitHub`].
*/
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArtifactProvider {
#[default]
GitHub,
}

impl ArtifactProvider {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::GitHub => "github",
}
}

#[must_use]
pub fn display_name(self) -> &'static str {
match self {
Self::GitHub => "GitHub",
}
}
}

impl FromStr for ArtifactProvider {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let l = s.trim().to_lowercase();
match l.as_str() {
"github" => Ok(Self::GitHub),
_ => Err(format!("unknown artifact provider '{l}'")),
}
}
}

impl fmt::Display for ArtifactProvider {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(f)
}
}

/**
An artifact format supported by Rokit.
*/
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArtifactFormat {
Zip,
Tar,
TarGz,
}

impl ArtifactFormat {
#[must_use]
pub fn as_str(&self) -> &'static str {
match self {
Self::Zip => "zip",
Self::Tar => "tar",
Self::TarGz => "tar.gz",
}
}

#[must_use]
pub fn from_extensions<'a>(extensions: impl AsRef<[&'a str]>) -> Option<Self> {
match extensions.as_ref() {
[.., ext] if ext.eq_ignore_ascii_case("zip") => Some(Self::Zip),
[.., ext] if ext.eq_ignore_ascii_case("tar") => Some(Self::Tar),
[.., ext] if ext.eq_ignore_ascii_case("tgz") => Some(Self::TarGz),
[.., ext1, ext2]
if ext1.eq_ignore_ascii_case("tar") && ext2.eq_ignore_ascii_case("gz") =>
{
Some(Self::TarGz)
}
_ => None,
}
}
mod format;
mod provider;
mod util;

#[must_use]
pub fn from_path_or_url(path_or_url: impl AsRef<str>) -> Option<Self> {
let path_or_url = path_or_url.as_ref();
let (_, extensions) = split_filename_and_extensions(path_or_url);
Self::from_extensions(extensions)
}
}
use self::util::split_filename_and_extensions;

impl FromStr for ArtifactFormat {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let l = s.trim().to_lowercase();
match l.as_str() {
"zip" => Ok(Self::Zip),
"tar" => Ok(Self::Tar),
"tar.gz" => Ok(Self::TarGz),
_ => Err(format!("unknown artifact format '{l}'")),
}
}
}

impl fmt::Display for ArtifactFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(f)
}
}
pub use self::format::ArtifactFormat;
pub use self::provider::ArtifactProvider;

/**
An artifact found by Rokit, to be downloaded and installed.
Expand Down Expand Up @@ -284,72 +184,3 @@ impl Artifact {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

fn format_from_str(s: &str) -> Option<ArtifactFormat> {
let (_, extensions) = split_filename_and_extensions(s);
ArtifactFormat::from_extensions(extensions)
}

#[test]
fn format_from_extensions_valid() {
assert_eq!(format_from_str("file.zip"), Some(ArtifactFormat::Zip));
assert_eq!(format_from_str("file.tar"), Some(ArtifactFormat::Tar));
assert_eq!(format_from_str("file.tar.gz"), Some(ArtifactFormat::TarGz));
assert_eq!(
format_from_str("file.with.many.extensions.tar.gz.zip"),
Some(ArtifactFormat::Zip)
);
assert_eq!(
format_from_str("file.with.many.extensions.zip.gz.tar"),
Some(ArtifactFormat::Tar)
);
assert_eq!(
format_from_str("file.with.many.extensions.tar.gz"),
Some(ArtifactFormat::TarGz)
);
}

#[test]
fn format_from_extensions_invalid() {
assert_eq!(format_from_str("file-name"), None);
assert_eq!(format_from_str("some/file.exe"), None);
assert_eq!(format_from_str("really.long.file.name"), None);
}

#[test]
fn format_from_real_tools() {
assert_eq!(
format_from_str("wally-v0.3.2-linux.zip"),
Some(ArtifactFormat::Zip)
);
assert_eq!(
format_from_str("lune-0.8.6-macos-aarch64.zip"),
Some(ArtifactFormat::Zip)
);
assert_eq!(
format_from_str("just-1.31.0-aarch64-apple-darwin.tar.gz"),
Some(ArtifactFormat::TarGz)
);
assert_eq!(
format_from_str("sentry-cli-linux-i686-2.32.1.tgz"),
Some(ArtifactFormat::TarGz)
);
}

#[test]
fn format_case_sensitivity() {
assert_eq!(format_from_str("file.ZIP"), Some(ArtifactFormat::Zip));
assert_eq!(format_from_str("file.zip"), Some(ArtifactFormat::Zip));
assert_eq!(format_from_str("file.Zip"), Some(ArtifactFormat::Zip));
assert_eq!(format_from_str("file.tar"), Some(ArtifactFormat::Tar));
assert_eq!(format_from_str("file.TAR"), Some(ArtifactFormat::Tar));
assert_eq!(format_from_str("file.Tar"), Some(ArtifactFormat::Tar));
assert_eq!(format_from_str("file.tar.gz"), Some(ArtifactFormat::TarGz));
assert_eq!(format_from_str("file.TAR.GZ"), Some(ArtifactFormat::TarGz));
assert_eq!(format_from_str("file.Tar.Gz"), Some(ArtifactFormat::TarGz));
}
}
Loading