Skip to content

Commit

Permalink
Merge pull request #10 from wizard-28/main
Browse files Browse the repository at this point in the history
Add unit tests, and fix some bugs
  • Loading branch information
wizard-28 committed May 2, 2022
2 parents b8ee4b1 + 1ae9f7f commit 4329368
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 52 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release

on:
workflow_dispatch

env:
CARGO_TERM_COLOR: always

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --release
- name: Move generated files
run: mv target/release/build/rhino-config-*/out/* target/release/
- uses: actions/upload-artifact@v3
with:
name: rhino-config
path: |
target/release/rhino-config
target/release/_rhino-config.ps1
target/release/rhino-config.1
target/release/rhino-config.bash
target/release/rhino-config.elv
target/release/rhino-config.fish
33 changes: 16 additions & 17 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ name: Rust

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]
workflow_dispatch:
inputs:
debug_enabled:
description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)'
description: "Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)"
required: false
default: false

Expand All @@ -17,21 +17,20 @@ env:

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Setup tmate session (Debug)
uses: mxschmitt/action-tmate@v3
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }}
- uses: actions/upload-artifact@v3
with:
name: rhino-config
path: |
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- uses: actions/upload-artifact@v3
with:
name: rhino-config
path: |
target/debug/rhino-config
target/debug/build/rhino-config-*/out/*
- name: Run tests
run: cargo test --verbose
- name: Setup tmate session (Debug)
uses: mxschmitt/action-tmate@v3
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }}

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ replit.nix
.replit

# Gitpod
gitpod.yml
gitpod.yml
setup.sh
86 changes: 86 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ indoc = "1.0.4"
clap = { version = "~3.1.10", features = ["derive"] }
clap_complete = "3.1.2"
clap_mangen = "0.1.6"

[dev-dependencies]
rstest = "0.12.0"
tempfile = "3.3.0"
127 changes: 116 additions & 11 deletions src/commands/disable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,147 @@ use std::fs;
use std::path::Path;
use std::process::Command;

use anyhow::{Context, Result};
use anyhow::{ensure, Context, Result};

pub fn mainline(config_path: &Path) -> Result<()> {
fs::remove_file(&config_path).context("Unable to remove mainline config file!")?;
println!("Mainline kernel has been disabled.");

Ok(())
}
pub fn pacstall(config_path: &Path) -> Result<()> {
fs::remove_file(&config_path).context("Unable to remove pacstall config file!")?;
println!("Pacstall has been disabled.");
println!("Removing pacstall...");

// Get the uninstall script from `curl` or `wget` depending upon which is
// installed on the system. Capture the output also.
let uninstall_script = if Command::new("curl").output()?.status.success() {
String::from_utf8(
Command::new("curl")
.args(["-fsSL", "https://git.io/JEZbi"])
.output()?
.stdout,
)?
} else {
String::from_utf8(
Command::new("wget")
.args(["-q", "https://git.io/JEZbi", "-O", "-"])
.output()?
.stdout,
)?
};
ensure!(Command::new("bash")
.args(["-c", &uninstall_script])
.status()?
.success());
Ok(())
}

pub fn snapdpurge(config_path: &Path) -> Result<()> {
fs::remove_file(&config_path).context("Unable to remove snapdpurge config file!")?;
println!("Snapdpurge has been disabled.");

println!("Reinstalling Snapcraft...");

Command::new("sudo")
ensure!(Command::new("sudo")
.args([
"apt",
"install",
"snapd",
"gnome-software-plugin-snap",
"-y",
])
.spawn()
.context("Unable to reinstall snapd!")?;
.status()
.context("Unable to reinstall snapd!")?
.success());

Command::new("sudo")
ensure!(Command::new("sudo")
.args(["apt-mark", "unhold", "snapd"])
.spawn()
.context("Unable to unhold snapd!")?;
.status()
.context("Unable to unhold snapd!")?
.success());

Ok(())
}

pub fn pacstall(config_path: &Path) -> Result<()> {
fs::remove_file(&config_path).context("Unable to remove pacstall config file!")?;
println!("Pacstall has been disabled.");
Ok(())
#[cfg(test)]
mod tests {
use std::env::var;
use std::error::Error;
use std::fs::File;
use std::process::Command;

use rstest::*;
use tempfile::{tempdir, TempDir};

#[fixture]
fn temp_dir() -> TempDir { tempdir().unwrap() }

#[rstest]
fn test_mainline(temp_dir: TempDir) -> Result<(), Box<dyn Error>> {
let config_path = temp_dir.path().join("mainline");
File::create(&config_path)?;

super::mainline(&config_path)?;
// Test that the config file is deleted
assert!(!config_path.exists());

Ok(())
}

#[rstest]
fn test_pacstall(temp_dir: TempDir) -> Result<(), Box<dyn Error>> {
let config_path = temp_dir.path().join("pacstall");
File::create(&config_path)?;

super::pacstall(&config_path)?;
// Test that the config file is deleted
assert!(!config_path.exists());

Ok(())
}

#[rstest]
fn test_snapdpurge(temp_dir: TempDir) -> Result<(), Box<dyn Error>> {
let config_path = temp_dir.path().join("snapdpurge");
File::create(&config_path)?;
let snapd_previously_installed = Command::new("dpkg")
.args(["--status", "snapd"])
.status()?
.success();

super::snapdpurge(&config_path)?;
// Test that the config file is deleted
assert!(!config_path.exists());
// Test that `snapd` and `gnome-software-plugin-snap` have been installed
assert!(Command::new("dpkg")
.args(["--status", "snapd", "gnome-software-plugin-snap"])
.status()?
.success());
// Test that `snapd` is unholded, i.e, it doesn't appear on `apt-mark showhold`
assert!(!String::from_utf8(
Command::new("sh")
.arg("apt-mark")
.arg("showhold")
.output()?
.stdout
)?
.contains("snapd"));

// Purge `snapd` and `gnome-software-plugin-snap` if previously not installed
// before test Don't run if the test is being run on a CI
if !snapd_previously_installed && var("CI").is_err() {
Command::new("sudo")
.args([
"apt",
"autopurge",
"snapd",
"gnome-software-plugin-snap",
"--assume-yes",
])
.status()?;
}
Ok(())
}
}
Loading

0 comments on commit 4329368

Please sign in to comment.