Skip to content

Commit

Permalink
Adding tests and fixing up types
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Oct 1, 2024
1 parent 3832a4d commit c56285f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
11 changes: 7 additions & 4 deletions crates/turborepo-lib/src/query/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl File {
}

// This is `Option` because the file may not be in the repo
async fn repo_relative_path(&self) -> Option<String> {
async fn path(&self) -> Option<String> {
self.run
.repo_root()
.anchor(&self.path)
Expand All @@ -101,8 +101,6 @@ impl File {
/// Gets the affected packages for the file, i.e. all packages that depend
/// on the file.
async fn affected_packages(&self) -> Result<Array<Package>, Error> {
// This is technically doable with the `package` field, but this is a nice bit
// of syntactic sugar
match self.get_package() {
Ok(Some(PackageMapping::All(_))) => Ok(self
.run
Expand All @@ -124,6 +122,11 @@ impl File {
run: self.run.clone(),
name: package.as_package_name().clone(),
})
// Add the package itself to the list
.chain(std::iter::once(Package {
run: self.run.clone(),
name: package.name.clone(),
}))
.sorted_by(|a, b| a.name.cmp(&b.name))
.collect())
}
Expand All @@ -132,7 +135,7 @@ impl File {
}
}

async fn file_dependencies(&self) -> Result<Vec<File>, Error> {
async fn file_dependencies(&self) -> Result<Array<File>, Error> {
let tracer = Tracer::new(
self.run.repo_root().to_owned(),
vec![self.path.clone()],
Expand Down
49 changes: 48 additions & 1 deletion crates/turborepo-lib/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod file;
mod package;
mod server;

use std::{io, sync::Arc};
use std::{borrow::Cow, io, sync::Arc};

use async_graphql::{http::GraphiQLSource, *};
use axum::{response, response::IntoResponse};
Expand All @@ -15,6 +15,7 @@ use tokio::select;
use turbo_trace::TraceError;
use turbopath::AbsoluteSystemPathBuf;
use turborepo_repository::package_graph::PackageName;
use turborepo_scm::git::ChangedFiles;

use crate::{
get_version,
Expand Down Expand Up @@ -45,6 +46,8 @@ pub enum Error {
#[error(transparent)]
UI(#[from] turborepo_ui::Error),
ChangeMapper(#[from] global_deps_package_change_mapper::Error),
#[error(transparent)]
Scm(#[from] turborepo_scm::Error),
}

pub struct RepositoryQuery {
Expand All @@ -58,6 +61,8 @@ impl RepositoryQuery {
}

#[derive(Debug, SimpleObject, Default)]
#[graphql(concrete(name = "Files", params(File)))]
#[graphql(concrete(name = "Packages", params(Package)))]
pub struct Array<T: OutputType> {
items: Vec<T>,
length: usize,
Expand All @@ -79,6 +84,13 @@ impl<T: OutputType> FromIterator<T> for Array<T> {
Self { items, length }
}
}

impl<T: OutputType> TypeName for Array<T> {
fn type_name() -> Cow<'static, str> {
Cow::Owned(format!("Array<{}>", T::type_name()))
}
}

#[derive(Enum, Copy, Clone, Eq, PartialEq, Debug)]
enum PackageFields {
Name,
Expand Down Expand Up @@ -346,6 +358,41 @@ impl RepositoryQuery {
Ok(File::new(self.run.clone(), abs_path))
}

async fn affected_files(
&self,
base: Option<String>,
head: Option<String>,
/// Defaults to true if `head` is not provided
include_uncommitted: Option<bool>,
/// Defaults to true
merge_base: Option<bool>,
) -> Result<Array<File>, Error> {
let base = base.as_deref();
let head = head.as_deref();
let include_uncommitted = include_uncommitted.unwrap_or_else(|| head.is_none());
let merge_base = merge_base.unwrap_or(true);
let repo_root = self.run.repo_root();
let change_result = self.run.scm().changed_files(
repo_root,
base,
head,
include_uncommitted,
false,
merge_base,
)?;

let files = match change_result {
// Shouldn't happen since we set `allow_unknown_objects` to false
ChangedFiles::All => unreachable!(),
ChangedFiles::Some(files) => files,
};

Ok(files
.into_iter()
.map(|file| File::new(self.run.clone(), self.run.repo_root().resolve(&file)))
.collect())
}

/// Gets a list of packages that match the given filter
async fn packages(&self, filter: Option<PackagePredicate>) -> Result<Array<Package>, Error> {
let Some(filter) = filter else {
Expand Down
27 changes: 27 additions & 0 deletions turborepo-tests/integration/tests/affected.t
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ Do the same thing with the `query` command
}
}

Also with `affectedFiles` in `turbo query`
$ ${TURBO} query "query { affectedFiles(base: \"main\", head: \"HEAD\") { items { path } } }"
{
"data": {
"affectedFiles": {
"items": [
{
"path": "apps/my-app/new.js"
}
]
}
}
}
Remove the new file
$ rm apps/my-app/new.js
Expand Down Expand Up @@ -104,6 +117,20 @@ Do the same thing with the `query` command
}
}
Also with `affectedFiles` in `turbo query`
$ ${TURBO} query "query { affectedFiles(base: \"main\", head: \"HEAD\") { items { path } } }"
{
"data": {
"affectedFiles": {
"items": [
{
"path": "apps/my-app/package.json"
}
]
}
}
}
Commit the change
$ git add .
$ git commit -m "add foo" --quiet
Expand Down

0 comments on commit c56285f

Please sign in to comment.