Skip to content

Commit

Permalink
Exercise spacetime proxy in smoketests
Browse files Browse the repository at this point in the history
  • Loading branch information
coolreader18 committed Jan 6, 2025
1 parent 699d806 commit 302d7d2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
38 changes: 34 additions & 4 deletions crates/update/src/proxy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::Context;
use spacetimedb_paths::RootDir;
use spacetimedb_paths::SpacetimePaths;
use spacetimedb_paths::{FromPathUnchecked, RootDir, SpacetimePaths};
use std::ffi::OsStr;
use std::ffi::OsString;
use std::path::PathBuf;
use std::process::Command;
use std::process::ExitCode;

Expand All @@ -14,8 +14,18 @@ pub(super) fn spacetimedb_cli_proxy(argv0: Option<&OsStr>, args: Vec<OsString>)
run_cli(&paths, argv0, args)
}
pub(crate) fn run_cli(paths: &SpacetimePaths, argv0: Option<&OsStr>, args: Vec<OsString>) -> anyhow::Result<ExitCode> {
let version = get_current_version();
let cli_path = paths.cli_bin_dir.version_dir(version).spacetimedb_cli();
let cli_path = if let Some(artifact_dir) = running_from_target_dir() {
let cli_path = spacetimedb_paths::cli::VersionBinDir::from_path_unchecked(artifact_dir).spacetimedb_cli();
anyhow::ensure!(
cli_path.0.exists(),
"running spacetimedb-update's cli proxy from a target/ directory, but the
spacetimedb-cli binary doesn't exist. try running `cargo build -p spacetimedb-cli`"
);
cli_path
} else {
let version = get_current_version();
paths.cli_bin_dir.version_dir(version).spacetimedb_cli()
};
let mut cmd = Command::new(&cli_path);
cmd.args(args);
#[cfg(unix)]
Expand All @@ -37,6 +47,26 @@ pub(crate) fn run_cli(paths: &SpacetimePaths, argv0: Option<&OsStr>, args: Vec<O
}
}

/// Checks to see if we're running from a subdirectory of a `target` dir that has a `Cargo.toml`
/// as a sibling, and returns the containing directory of the current executable if so.
fn running_from_target_dir() -> Option<PathBuf> {
let mut exe_path = std::env::current_exe().ok()?;
exe_path.pop();
let artifact_dir = exe_path;
// check for target/debug/spacetimedb-update and target/x86_64-unknown-foobar/debug/spacetimedb-update
let target_dir = artifact_dir
.ancestors()
.skip(1)
.take(2)
.find(|p| p.file_name() == Some("target".as_ref()))?;
target_dir
.parent()?
.join("Cargo.toml")
.try_exists()
.ok()
.map(|_| artifact_dir)
}

fn get_current_version() -> semver::Version {
// TODO:
"1.0.0".parse().unwrap()
Expand Down
2 changes: 1 addition & 1 deletion smoketests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# miscellaneous file paths
TEST_DIR = Path(__file__).parent
STDB_DIR = TEST_DIR.parent
SPACETIME_BIN = STDB_DIR / "target/debug/spacetimedb-cli"
SPACETIME_BIN = STDB_DIR / "target/debug/spacetime"
TEMPLATE_TARGET_DIR = STDB_DIR / "target/_stdbsmoketests"
STDB_CONFIG = TEST_DIR / "config.toml"

Expand Down
19 changes: 17 additions & 2 deletions smoketests/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import re
import fnmatch
import json
from . import TEST_DIR, build_template_target
from . import TEST_DIR, SPACETIME_BIN, build_template_target
import smoketests
import logging

Expand Down Expand Up @@ -68,7 +68,22 @@ def main():
args = parser.parse_args()

logging.info("Compiling spacetime cli...")
smoketests.run_cmd("cargo", "build", cwd=TEST_DIR.parent, capture_stderr=False)
smoketests.run_cmd("cargo", "build", "-pspacetimedb-cli", "-pspacetimedb-update", cwd=TEST_DIR.parent, capture_stderr=False)

try:
bin_is_symlink = SPACETIME_BIN.readlink() == "spacetimedb-update"
except OSError:
bin_is_symlink = False
if not bin_is_symlink:
try:
os.remove(SPACETIME_BIN)
except FileNotFoundError:
pass
try:
os.symlink("spacetimedb-update", SPACETIME_BIN)
except OSError:
import shutil
shutil.copyfile(SPACETIME_BIN.with_name("spacetimedb-update"), SPACETIME_BIN)

os.environ["SPACETIME_SKIP_CLIPPY"] = "1"

Expand Down

0 comments on commit 302d7d2

Please sign in to comment.