test,xtask: Replace lazy_static with OnceCell

This doesn't affect MSRV because this is testing code.
pull/639/head
Tamir Duberstein 1 year ago
parent ff86f1385c
commit 5a2906a6c9
No known key found for this signature in database

@ -14,7 +14,6 @@ env_logger = "0.10"
futures-core = "0.3"
inventory = "0.3"
integration-test-macros = { path = "../integration-test-macros" }
lazy_static = "1"
libc = { version = "0.2.105" }
log = "0.4"
object = { version = "0.31", default-features = false, features = ["std", "read_core", "elf"] }

@ -1,8 +1,7 @@
use anyhow::bail;
use lazy_static::lazy_static;
use libc::{uname, utsname};
use regex::Regex;
use std::{ffi::CStr, mem};
use std::{cell::OnceCell, ffi::CStr, mem};
pub mod bpf_probe_read;
pub mod btf_relocations;
@ -22,15 +21,15 @@ pub struct IntegrationTest {
}
pub(crate) fn kernel_version() -> anyhow::Result<(u8, u8, u8)> {
lazy_static! {
static ref RE: Regex = Regex::new(r"^([0-9]+)\.([0-9]+)\.([0-9]+)").unwrap();
}
static mut RE: OnceCell<Regex> = OnceCell::new();
let re =
unsafe { &mut RE }.get_or_init(|| Regex::new(r"^([0-9]+)\.([0-9]+)\.([0-9]+)").unwrap());
let mut data: utsname = unsafe { mem::zeroed() };
let ret = unsafe { uname(&mut data) };
assert!(ret >= 0, "libc::uname failed.");
let release_cstr = unsafe { CStr::from_ptr(data.release.as_ptr()) };
let release = release_cstr.to_string_lossy();
if let Some(caps) = RE.captures(&release) {
if let Some(caps) = re.captures(&release) {
let major = caps.get(1).unwrap().as_str().parse().unwrap();
let minor = caps.get(2).unwrap().as_str().parse().unwrap();
let patch = caps.get(3).unwrap().as_str().parse().unwrap();

@ -12,5 +12,4 @@ syn = "2"
quote = "1"
proc-macro2 = "1"
indoc = "2.0"
lazy_static = "1"
serde_json = "1"

@ -7,7 +7,7 @@ use std::{
use anyhow::{bail, Context};
use clap::Parser;
use crate::utils::WORKSPACE_ROOT;
use crate::utils::workspace_root;
#[derive(Debug, Copy, Clone)]
pub enum Architecture {
@ -52,7 +52,7 @@ pub fn build_ebpf(opts: BuildEbpfOptions) -> anyhow::Result<()> {
}
fn build_rust_ebpf(opts: &BuildEbpfOptions) -> anyhow::Result<()> {
let mut dir = PathBuf::from(WORKSPACE_ROOT.to_string());
let mut dir = PathBuf::from(workspace_root());
dir.push("test/integration-ebpf");
let target = format!("--target={}", opts.target);
@ -88,10 +88,10 @@ fn get_libbpf_headers<P: AsRef<Path>>(libbpf_dir: P, include_path: P) -> anyhow:
}
fn build_c_ebpf(opts: &BuildEbpfOptions) -> anyhow::Result<()> {
let mut src = PathBuf::from(WORKSPACE_ROOT.to_string());
let mut src = PathBuf::from(workspace_root());
src.push("test/integration-ebpf/src/bpf");
let mut out_path = PathBuf::from(WORKSPACE_ROOT.to_string());
let mut out_path = PathBuf::from(workspace_root());
out_path.push("target");
out_path.push(opts.target.to_string());
out_path.push("release");

@ -1,17 +1,15 @@
use lazy_static::lazy_static;
use serde_json::Value;
use std::process::Command;
use std::{cell::OnceCell, process::Command};
lazy_static! {
pub static ref WORKSPACE_ROOT: String = workspace_root();
}
fn workspace_root() -> String {
let output = Command::new("cargo").arg("metadata").output().unwrap();
if !output.status.success() {
panic!("unable to run cargo metadata")
}
let stdout = String::from_utf8(output.stdout).unwrap();
let v: Value = serde_json::from_str(&stdout).unwrap();
v["workspace_root"].as_str().unwrap().to_string()
pub fn workspace_root() -> &'static str {
static mut WORKSPACE_ROOT: OnceCell<String> = OnceCell::new();
unsafe { &mut WORKSPACE_ROOT }.get_or_init(|| {
let output = Command::new("cargo").arg("metadata").output().unwrap();
if !output.status.success() {
panic!("unable to run cargo metadata")
}
let stdout = String::from_utf8(output.stdout).unwrap();
let v: Value = serde_json::from_str(&stdout).unwrap();
v["workspace_root"].as_str().unwrap().to_string()
})
}

Loading…
Cancel
Save