Skip to content

Commit

Permalink
Merge pull request #1354 from axodotdev/homebrew_style_lint_tests
Browse files Browse the repository at this point in the history
tests: enable `brew style` in linters
  • Loading branch information
mistydemeo authored Aug 21, 2024
2 parents a663e04 + ea8ec8e commit 921905e
Show file tree
Hide file tree
Showing 38 changed files with 419 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@

export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
brew update
brew style --fix "Formula/${filename}" || true
# We avoid reformatting user-provided data such as the app description and homepage.
brew style --except-cops FormulaAudit/Homepage,FormulaAudit/Desc,FormulaAuditStrict --fix "Formula/${filename}" || true

git add "Formula/${filename}"
git commit -m "${name} ${version}"
Expand Down
2 changes: 1 addition & 1 deletion cargo-dist/templates/installer/homebrew.rb.j2
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class {{ formula_class }} < Formula
{%- endfor %}
{%- endif %}

BINARY_ALIASES = {{ inner.bin_aliases }}
BINARY_ALIASES = {{ inner.bin_aliases | tojson(indent=2) | indent(2) }}

def target_triple
cpu = Hardware::CPU.arm? ? "aarch64" : "x86_64"
Expand Down
65 changes: 58 additions & 7 deletions cargo-dist/tests/gallery/dist/homebrew.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{path::PathBuf, process::Output};

use super::*;

impl AppResult {
Expand All @@ -24,15 +26,32 @@ impl AppResult {
return Ok(());
};

// The ./ at the start ensures Homebrew sees this as a path
// reference and doesn't misinrepret it as a reference to a
// formula in a tap.
let relative_formula_path = format!("./{formula_path}");
// Homebrew fails to guess that this is a formula
// file if it's not in a path named Formula,
// so we need to put the formula in a temp path
// to hint it correctly.
// (We could also skip individual lints via
// --except-cop on the `brew style` CLI, but that's
// a bit too much of a game of whack a mole.)
let temp_root = temp_dir::TempDir::new().unwrap();
let formula_temp_path = create_formula_copy(&temp_root, formula_path).unwrap();

// We perform linting here too because we want to both
// lint and runtest the `brew style --fix`ed version.
// We're unable to check the fixed version into the
// snapshots since it doesn't work cross-platform, so
// doing them both in one place means we don't have to
// run it twice.
let output = brew_style(homebrew, &formula_temp_path)?;
if !output.status.success() {
eprintln!("{}", String::from_utf8_lossy(&output.stdout));
return Err(miette!("brew style found issues"));
}

eprintln!("running brew install...");
homebrew.output_checked(|cmd| cmd.arg("install").arg(&relative_formula_path))?;
homebrew.output_checked(|cmd| cmd.arg("install").arg(&formula_temp_path))?;
let prefix_output =
homebrew.output_checked(|cmd| cmd.arg("--prefix").arg(&relative_formula_path))?;
homebrew.output_checked(|cmd| cmd.arg("--prefix").arg(&formula_temp_path))?;
let prefix_raw = String::from_utf8(prefix_output.stdout).unwrap();
let prefix = prefix_raw.strip_suffix('\n').unwrap();
let bin = Utf8PathBuf::from(&prefix).join("bin");
Expand All @@ -42,8 +61,40 @@ impl AppResult {
assert!(bin_path.exists(), "bin wasn't created");
}

homebrew.output_checked(|cmd| cmd.arg("uninstall").arg(relative_formula_path))?;
homebrew.output_checked(|cmd| cmd.arg("uninstall").arg(formula_temp_path))?;
}
Ok(())
}
}

fn create_formula_copy(
temp_root: &temp_dir::TempDir,
formula_path: &Utf8PathBuf,
) -> std::io::Result<PathBuf> {
let formula_temp_root = temp_root.path().join("Formula");
std::fs::create_dir(&formula_temp_root)?;
let formula_temp_path = formula_temp_root.join(formula_path.file_name().unwrap());
std::fs::copy(formula_path, &formula_temp_path)?;

Ok(formula_temp_path)
}

fn brew_style(homebrew: &CommandInfo, path: &PathBuf) -> Result<Output> {
homebrew.output(|cmd| {
cmd.arg("style")
// We ignore audits for user-supplied metadata,
// since we avoid rewriting those on behalf of
// the user. We also avoid the homepage nit,
// because if the user doesn't supply a homepage
// it's correct that we don't generate one.
// We add FormulaAuditStrict because that's the
// default exclusion, and adding anything to
// --except-cops overrides it.
.arg("--except-cops")
.arg("FormulaAudit/Homepage,FormulaAudit/Desc,FormulaAuditStrict")
// Applying --fix will ensure that fixable
// style issues won't be treated as errors.
.arg("--fix")
.arg(path)
})
}
10 changes: 8 additions & 2 deletions cargo-dist/tests/snapshots/akaikatana_basic.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,12 @@ class AkaikatanaRepack < Formula
end
license "GPL-2.0-or-later"

BINARY_ALIASES = {"aarch64-apple-darwin": {}, "x86_64-apple-darwin": {}, "x86_64-pc-windows-gnu": {}, "x86_64-unknown-linux-gnu": {}}
BINARY_ALIASES = {
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"x86_64-pc-windows-gnu": {},
"x86_64-unknown-linux-gnu": {}
}

def target_triple
cpu = Hardware::CPU.arm? ? "aarch64" : "x86_64"
Expand Down Expand Up @@ -2272,7 +2277,8 @@ jobs:
export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
brew update
brew style --fix "Formula/${filename}" || true
# We avoid reformatting user-provided data such as the app description and homepage.
brew style --except-cops FormulaAudit/Homepage,FormulaAudit/Desc,FormulaAuditStrict --fix "Formula/${filename}" || true
git add "Formula/${filename}"
git commit -m "${name} ${version}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,28 @@ class AkaikatanaRepack < Formula
end
license "GPL-2.0-or-later"

BINARY_ALIASES = {"aarch64-apple-darwin": {"akextract": ["akextract-link"]}, "x86_64-apple-darwin": {"akextract": ["akextract-link"]}, "x86_64-pc-windows-gnu": {"akextract.exe": ["akextract-link.exe"]}, "x86_64-unknown-linux-gnu": {"akextract": ["akextract-link"]}}
BINARY_ALIASES = {
"aarch64-apple-darwin": {
"akextract": [
"akextract-link"
]
},
"x86_64-apple-darwin": {
"akextract": [
"akextract-link"
]
},
"x86_64-pc-windows-gnu": {
"akextract.exe": [
"akextract-link.exe"
]
},
"x86_64-unknown-linux-gnu": {
"akextract": [
"akextract-link"
]
}
}

def target_triple
cpu = Hardware::CPU.arm? ? "aarch64" : "x86_64"
Expand Down Expand Up @@ -2286,7 +2307,8 @@ jobs:
export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
brew update
brew style --fix "Formula/${filename}" || true
# We avoid reformatting user-provided data such as the app description and homepage.
brew style --except-cops FormulaAudit/Homepage,FormulaAudit/Desc,FormulaAuditStrict --fix "Formula/${filename}" || true
git add "Formula/${filename}"
git commit -m "${name} ${version}"
Expand Down
38 changes: 36 additions & 2 deletions cargo-dist/tests/snapshots/akaikatana_two_bin_aliases.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,40 @@ class AkaikatanaRepack < Formula
end
license "GPL-2.0-or-later"

BINARY_ALIASES = {"aarch64-apple-darwin": {"akextract": ["akextract-link"], "akmetadata": ["akmetadata-link"]}, "x86_64-apple-darwin": {"akextract": ["akextract-link"], "akmetadata": ["akmetadata-link"]}, "x86_64-pc-windows-gnu": {"akextract.exe": ["akextract-link.exe"], "akmetadata.exe": ["akmetadata-link.exe"]}, "x86_64-unknown-linux-gnu": {"akextract": ["akextract-link"], "akmetadata": ["akmetadata-link"]}}
BINARY_ALIASES = {
"aarch64-apple-darwin": {
"akextract": [
"akextract-link"
],
"akmetadata": [
"akmetadata-link"
]
},
"x86_64-apple-darwin": {
"akextract": [
"akextract-link"
],
"akmetadata": [
"akmetadata-link"
]
},
"x86_64-pc-windows-gnu": {
"akextract.exe": [
"akextract-link.exe"
],
"akmetadata.exe": [
"akmetadata-link.exe"
]
},
"x86_64-unknown-linux-gnu": {
"akextract": [
"akextract-link"
],
"akmetadata": [
"akmetadata-link"
]
}
}

def target_triple
cpu = Hardware::CPU.arm? ? "aarch64" : "x86_64"
Expand Down Expand Up @@ -2300,7 +2333,8 @@ jobs:
export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
brew update
brew style --fix "Formula/${filename}" || true
# We avoid reformatting user-provided data such as the app description and homepage.
brew style --except-cops FormulaAudit/Homepage,FormulaAudit/Desc,FormulaAuditStrict --fix "Formula/${filename}" || true
git add "Formula/${filename}"
git commit -m "${name} ${version}"
Expand Down
10 changes: 8 additions & 2 deletions cargo-dist/tests/snapshots/akaikatana_updaters.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,12 @@ class AkaikatanaRepack < Formula
end
license "GPL-2.0-or-later"

BINARY_ALIASES = {"aarch64-apple-darwin": {}, "x86_64-apple-darwin": {}, "x86_64-pc-windows-gnu": {}, "x86_64-unknown-linux-gnu": {}}
BINARY_ALIASES = {
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"x86_64-pc-windows-gnu": {},
"x86_64-unknown-linux-gnu": {}
}

def target_triple
cpu = Hardware::CPU.arm? ? "aarch64" : "x86_64"
Expand Down Expand Up @@ -2312,7 +2317,8 @@ jobs:
export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
brew update
brew style --fix "Formula/${filename}" || true
# We avoid reformatting user-provided data such as the app description and homepage.
brew style --except-cops FormulaAudit/Homepage,FormulaAudit/Desc,FormulaAuditStrict --fix "Formula/${filename}" || true
git add "Formula/${filename}"
git commit -m "${name} ${version}"
Expand Down
7 changes: 6 additions & 1 deletion cargo-dist/tests/snapshots/axolotlsay_abyss.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,12 @@ class Axolotlsay < Formula
end
license any_of: ["MIT", "Apache-2.0"]

BINARY_ALIASES = {"aarch64-apple-darwin": {}, "x86_64-apple-darwin": {}, "x86_64-pc-windows-gnu": {}, "x86_64-unknown-linux-gnu": {}}
BINARY_ALIASES = {
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"x86_64-pc-windows-gnu": {},
"x86_64-unknown-linux-gnu": {}
}

def target_triple
cpu = Hardware::CPU.arm? ? "aarch64" : "x86_64"
Expand Down
7 changes: 6 additions & 1 deletion cargo-dist/tests/snapshots/axolotlsay_abyss_only.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,12 @@ class Axolotlsay < Formula
end
license any_of: ["MIT", "Apache-2.0"]

BINARY_ALIASES = {"aarch64-apple-darwin": {}, "x86_64-apple-darwin": {}, "x86_64-pc-windows-gnu": {}, "x86_64-unknown-linux-gnu": {}}
BINARY_ALIASES = {
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"x86_64-pc-windows-gnu": {},
"x86_64-unknown-linux-gnu": {}
}

def target_triple
cpu = Hardware::CPU.arm? ? "aarch64" : "x86_64"
Expand Down
26 changes: 24 additions & 2 deletions cargo-dist/tests/snapshots/axolotlsay_alias.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,28 @@ class Axolotlsay < Formula
end
license any_of: ["MIT", "Apache-2.0"]

BINARY_ALIASES = {"aarch64-apple-darwin": {"axolotlsay": ["axolotlsay-link"]}, "x86_64-apple-darwin": {"axolotlsay": ["axolotlsay-link"]}, "x86_64-pc-windows-gnu": {"axolotlsay.exe": ["axolotlsay-link.exe"]}, "x86_64-unknown-linux-gnu": {"axolotlsay": ["axolotlsay-link"]}}
BINARY_ALIASES = {
"aarch64-apple-darwin": {
"axolotlsay": [
"axolotlsay-link"
]
},
"x86_64-apple-darwin": {
"axolotlsay": [
"axolotlsay-link"
]
},
"x86_64-pc-windows-gnu": {
"axolotlsay.exe": [
"axolotlsay-link.exe"
]
},
"x86_64-unknown-linux-gnu": {
"axolotlsay": [
"axolotlsay-link"
]
}
}

def target_triple
cpu = Hardware::CPU.arm? ? "aarch64" : "x86_64"
Expand Down Expand Up @@ -3831,7 +3852,8 @@ jobs:
export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
brew update
brew style --fix "Formula/${filename}" || true
# We avoid reformatting user-provided data such as the app description and homepage.
brew style --except-cops FormulaAudit/Homepage,FormulaAudit/Desc,FormulaAuditStrict --fix "Formula/${filename}" || true
git add "Formula/${filename}"
git commit -m "${name} ${version}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,32 @@ class Axolotlsay < Formula
end
license any_of: ["MIT", "Apache-2.0"]

BINARY_ALIASES = {"aarch64-apple-darwin": {"nosuchbin": ["axolotlsay-link1", "axolotlsay-link2"]}, "x86_64-apple-darwin": {"nosuchbin": ["axolotlsay-link1", "axolotlsay-link2"]}, "x86_64-pc-windows-gnu": {"nosuchbin.exe": ["axolotlsay-link1.exe", "axolotlsay-link2.exe"]}, "x86_64-unknown-linux-gnu": {"nosuchbin": ["axolotlsay-link1", "axolotlsay-link2"]}}
BINARY_ALIASES = {
"aarch64-apple-darwin": {
"nosuchbin": [
"axolotlsay-link1",
"axolotlsay-link2"
]
},
"x86_64-apple-darwin": {
"nosuchbin": [
"axolotlsay-link1",
"axolotlsay-link2"
]
},
"x86_64-pc-windows-gnu": {
"nosuchbin.exe": [
"axolotlsay-link1.exe",
"axolotlsay-link2.exe"
]
},
"x86_64-unknown-linux-gnu": {
"nosuchbin": [
"axolotlsay-link1",
"axolotlsay-link2"
]
}
}

def target_triple
cpu = Hardware::CPU.arm? ? "aarch64" : "x86_64"
Expand Down Expand Up @@ -3829,7 +3854,8 @@ jobs:
export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
brew update
brew style --fix "Formula/${filename}" || true
# We avoid reformatting user-provided data such as the app description and homepage.
brew style --except-cops FormulaAudit/Homepage,FormulaAudit/Desc,FormulaAuditStrict --fix "Formula/${filename}" || true
git add "Formula/${filename}"
git commit -m "${name} ${version}"
Expand Down
10 changes: 8 additions & 2 deletions cargo-dist/tests/snapshots/axolotlsay_basic.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,12 @@ class Axolotlsay < Formula
end
license any_of: ["MIT", "Apache-2.0"]

BINARY_ALIASES = {"aarch64-apple-darwin": {}, "x86_64-apple-darwin": {}, "x86_64-pc-windows-gnu": {}, "x86_64-unknown-linux-gnu": {}}
BINARY_ALIASES = {
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"x86_64-pc-windows-gnu": {},
"x86_64-unknown-linux-gnu": {}
}

def target_triple
cpu = Hardware::CPU.arm? ? "aarch64" : "x86_64"
Expand Down Expand Up @@ -3815,7 +3820,8 @@ jobs:
export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
brew update
brew style --fix "Formula/${filename}" || true
# We avoid reformatting user-provided data such as the app description and homepage.
brew style --except-cops FormulaAudit/Homepage,FormulaAudit/Desc,FormulaAuditStrict --fix "Formula/${filename}" || true
git add "Formula/${filename}"
git commit -m "${name} ${version}"
Expand Down
Loading

0 comments on commit 921905e

Please sign in to comment.