Skip to content

Commit

Permalink
chore(xtask): refactor tool runners & e2e test supergraph demo (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper authored Jun 23, 2021
1 parent b6d8497 commit ec747e8
Show file tree
Hide file tree
Showing 18 changed files with 299 additions and 165 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2018"

[dependencies]
ansi_term = "0.12"
assert_fs = "1"
anyhow = "1"
camino = "1.0"
cargo_metadata = "0.13"
Expand Down
5 changes: 0 additions & 5 deletions xtask/src/commands/cargo/mod.rs

This file was deleted.

11 changes: 4 additions & 7 deletions xtask/src/commands/dist/mod.rs → xtask/src/commands/dist.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
mod strip;

use crate::commands::{CargoRunner, Target, POSSIBLE_TARGETS};

use anyhow::{Context, Result};
use structopt::StructOpt;

use crate::commands::dist::strip::StripRunner;
use crate::target::{Target, POSSIBLE_TARGETS};
use crate::tools::{CargoRunner, StripRunner};

#[derive(Debug, StructOpt)]
pub struct Dist {
Expand All @@ -17,11 +14,11 @@ impl Dist {
pub fn run(&self, verbose: bool) -> Result<()> {
let cargo_runner = CargoRunner::new(verbose)?;
let binary_path = cargo_runner
.build(self.target.to_owned())
.build(&self.target, true)
.with_context(|| "Could not build Rover.")?;

if !cfg!(windows) {
let strip_runner = StripRunner::new(binary_path, verbose);
let strip_runner = StripRunner::new(binary_path, verbose)?;
strip_runner
.run()
.with_context(|| "Could not strip symbols from Rover's binary")?;
Expand Down
2 changes: 1 addition & 1 deletion xtask/src/commands/lint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use structopt::StructOpt;

use crate::commands::CargoRunner;
use crate::tools::CargoRunner;

#[derive(Debug, StructOpt)]
pub struct Lint {}
Expand Down
2 changes: 0 additions & 2 deletions xtask/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
mod cargo;
pub(crate) mod dist;
pub(crate) mod lint;
pub(crate) mod prep;
pub(crate) mod test;

pub(crate) use cargo::{CargoRunner, Target, POSSIBLE_TARGETS};
pub(crate) use dist::Dist;
pub(crate) use lint::Lint;
pub(crate) use prep::Prep;
Expand Down
5 changes: 3 additions & 2 deletions xtask/src/commands/prep/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
mod docs;
mod installers;
mod npm;

use anyhow::{Context, Result};
use structopt::StructOpt;

use crate::commands::prep::docs::DocsRunner;
use crate::tools::NpmRunner;

#[derive(Debug, StructOpt)]
pub struct Prep {}

impl Prep {
pub fn run(&self, verbose: bool) -> Result<()> {
npm::prepare_package(verbose)?;
let npm_runner = NpmRunner::new(verbose)?;
npm_runner.prepare_package()?;
installers::update_versions()?;
let docs_runner = DocsRunner::new()?;
docs_runner
Expand Down
17 changes: 15 additions & 2 deletions xtask/src/commands/test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::Result;
use structopt::StructOpt;

use crate::commands::CargoRunner;
use crate::commands::{Target, POSSIBLE_TARGETS};
use crate::target::{Target, POSSIBLE_TARGETS};
use crate::tools::{CargoRunner, GitRunner, MakeRunner};

#[derive(Debug, StructOpt)]
pub struct Test {
Expand All @@ -12,8 +12,21 @@ pub struct Test {

impl Test {
pub fn run(&self, verbose: bool) -> Result<()> {
let release = false;
let cargo_runner = CargoRunner::new(verbose)?;
let git_runner = GitRunner::new(verbose)?;

cargo_runner.test(self.target.to_owned())?;

if let Target::GnuLinux = self.target {
let make_runner =
MakeRunner::new(verbose, cargo_runner.get_bin_path(&self.target, release))?;
cargo_runner.build(&self.target, release)?;

let repo_path = git_runner.clone_supergraph_demo()?;
make_runner.test_supergraph_demo(&repo_path)?;
}

Ok(())
}
}
3 changes: 3 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
mod commands;

pub(crate) mod target;
pub(crate) mod tools;
pub(crate) mod utils;

use ansi_term::Colour::Green;
Expand Down
File renamed without changes.
49 changes: 28 additions & 21 deletions xtask/src/commands/cargo/runner.rs → xtask/src/tools/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@ use std::{collections::HashMap, str::FromStr};
use anyhow::{anyhow, Result};
use camino::Utf8PathBuf;

use crate::commands::Target;
use crate::target::Target;
use crate::tools::Runner;
use crate::utils::{self, CommandOutput};

pub(crate) struct CargoRunner {
rover_package_directory: Utf8PathBuf,
verbose: bool,
cargo_package_directory: Utf8PathBuf,
runner: Runner,
}

impl CargoRunner {
pub(crate) fn new(verbose: bool) -> Result<Self> {
let rover_package_directory = utils::project_root()?;
let runner = Runner::new("cargo", verbose)?;
let cargo_package_directory = utils::project_root()?;

Ok(CargoRunner {
rover_package_directory,
verbose,
cargo_package_directory,
runner,
})
}

pub(crate) fn build(&self, target: Target) -> Result<Utf8PathBuf> {
pub(crate) fn build(&self, target: &Target, release: bool) -> Result<Utf8PathBuf> {
let target_str = target.to_string();
let mut args = vec!["build", "--release", "--target", &target_str];
let mut args = vec!["build", "--target", &target_str];
if release {
args.push("--release");
}
if !target.composition_js() {
args.push("--no-default-features");
}
Expand All @@ -49,12 +54,7 @@ impl CargoRunner {
}
}
self.cargo_exec(&args, Some(env))?;
Ok(self
.rover_package_directory
.join("target")
.join(&target_str)
.join("release")
.join("rover"))
Ok(self.get_bin_path(target, release))
}

pub(crate) fn lint(&self) -> Result<()> {
Expand Down Expand Up @@ -87,17 +87,24 @@ impl CargoRunner {
Ok(())
}

pub(crate) fn get_bin_path(&self, target: &Target, release: bool) -> Utf8PathBuf {
let mut path = self.cargo_package_directory.clone();
path.push("target");
path.push(target.to_string());
if release {
path.push("release")
} else {
path.push("debug")
}
path.push("rover");
path
}

fn cargo_exec(
&self,
args: &[&str],
env: Option<HashMap<String, String>>,
) -> Result<CommandOutput> {
utils::exec(
"cargo",
args,
&self.rover_package_directory,
self.verbose,
env,
)
self.runner.exec(args, &self.cargo_package_directory, env)
}
}
40 changes: 40 additions & 0 deletions xtask/src/tools/git.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::tools::Runner;

use std::convert::TryFrom;

use anyhow::{Context, Result};
use assert_fs::TempDir;
use camino::Utf8PathBuf;

pub(crate) struct GitRunner {
temp_dir_path: Utf8PathBuf,
runner: Runner,

// we store _temp_dir here since its Drop implementation deletes the directory
_temp_dir: TempDir,
}

impl GitRunner {
pub(crate) fn new(verbose: bool) -> Result<Self> {
let runner = Runner::new("git", verbose)?;
let temp_dir = TempDir::new().with_context(|| "Could not create temp directory")?;
let temp_dir_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf())
.with_context(|| "Temp directory was not valid Utf-8")?;

Ok(GitRunner {
runner,
temp_dir_path,
_temp_dir: temp_dir,
})
}

pub(crate) fn clone_supergraph_demo(&self) -> Result<Utf8PathBuf> {
let repo_name = "supergraph-demo";
let repo_url = format!("https://github.com/apollographql/{}", repo_name);
self.runner
.exec(&["clone", &repo_url], &self.temp_dir_path, None)?;

let repo_path = self.temp_dir_path.join(repo_name);
Ok(repo_path)
}
}
54 changes: 54 additions & 0 deletions xtask/src/tools/make.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::collections::HashMap;

use crate::tools::Runner;
use crate::utils::CommandOutput;

use anyhow::{anyhow, Context, Result};
use camino::{Utf8Path, Utf8PathBuf};

pub(crate) struct MakeRunner {
runner: Runner,
rover_exe: Utf8PathBuf,
}

impl MakeRunner {
pub(crate) fn new(verbose: bool, rover_exe: Utf8PathBuf) -> Result<Self> {
let runner = Runner::new("make", verbose)?;

Ok(MakeRunner { runner, rover_exe })
}

pub(crate) fn test_supergraph_demo(&self, base_dir: &Utf8Path) -> Result<()> {
let mut env = HashMap::new();
env.insert("ROVER_BIN".to_string(), self.rover_exe.to_string());
let output = self.runner.exec(&["ci"], base_dir, Some(env))?;
assert_demo_includes(&output)
.with_context(|| "There were problems with the output of 'make ci'.")
}
}

fn assert_demo_includes(output: &CommandOutput) -> Result<()> {
let necessary_stdout = vec!["🚀 Graph Router ready at http://localhost:4000/"];
let necessary_stderr = vec!["allProducts", "Removing network"];

let mut missing_strings = Vec::with_capacity(necessary_stderr.len() + necessary_stdout.len());
for necessary_string in necessary_stdout {
if !output.stdout.contains(necessary_string) {
missing_strings.push(necessary_string);
}
}
for necessary_string in necessary_stderr {
if !output.stderr.contains(necessary_string) {
missing_strings.push(necessary_string);
}
}

if missing_strings.is_empty() {
Ok(())
} else {
Err(anyhow!(
"The output from 'make` is missing the following strings: {:?}",
missing_strings
))
}
}
13 changes: 13 additions & 0 deletions xtask/src/tools/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod cargo;
mod git;
mod make;
mod npm;
mod runner;
mod strip;

pub(crate) use cargo::CargoRunner;
pub(crate) use git::GitRunner;
pub(crate) use make::MakeRunner;
pub(crate) use npm::NpmRunner;
pub(crate) use runner::Runner;
pub(crate) use strip::StripRunner;
Loading

0 comments on commit ec747e8

Please sign in to comment.