use std::{ collections::{BTreeMap, HashMap, HashSet}, ops::{Deref, DerefMut}, sync::Arc, }; use biome_deserialize_macros::Deserializable; use camino::Utf8Path; use clap::ValueEnum; use miette::{NamedSource, SourceSpan}; use serde::{Deserialize, Serialize}; use struct_iterable::Iterable; use turbopath::AbsoluteSystemPath; use turborepo_errors::Spanned; use turborepo_micro_frontend::MICRO_FRONTENDS_PACKAGE_INTERNAL; use turborepo_repository::package_graph::ROOT_PKG_NAME; use turborepo_unescape::UnescapedString; use crate::{ cli::{EnvMode, OutputLogsMode}, config::{ConfigurationOptions, Error, InvalidEnvPrefixError}, run::{ task_access::TaskAccessTraceFile, task_id::{TaskId, TaskName}, }, task_graph::{TaskDefinition, TaskOutputs}, }; mod loader; pub mod parser; pub use loader::TurboJsonLoader; #[derive(Serialize, Deserialize, Debug, Default, PartialEq, Clone, Deserializable)] #[serde(rename_all = "camelCase")] pub struct SpacesJson { pub id: Option, } // A turbo.json config that is synthesized but not yet resolved. // This means that we've done the work to synthesize the config from // package.json, but we haven't yet resolved the workspace // turbo.json files into a single definition. Therefore we keep the // `RawTaskDefinition` type so we can determine which fields are actually // set when we resolve the configuration. // // Note that the values here are limited to pipeline configuration. // Configuration that needs to account for flags, env vars, etc. is // handled via layered config. #[derive(Debug, Default, Clone, PartialEq)] pub struct TurboJson { text: Option>, path: Option>, pub(crate) extends: Spanned>, pub(crate) global_deps: Vec, pub(crate) global_env: Vec, pub(crate) global_pass_through_env: Option>, pub(crate) tasks: Pipeline, } // Iterable is required to enumerate allowed keys #[derive(Clone, Debug, Default, Iterable, Serialize, Deserializable)] #[serde(rename_all = "camelCase")] pub(crate) struct RawRemoteCacheOptions { #[serde(skip_serializing_if = "Option::is_none")] api_url: Option, #[serde(skip_serializing_if = "Option::is_none")] login_url: Option, #[serde(skip_serializing_if = "Option::is_none")] team_slug: Option, #[serde(skip_serializing_if = "Option::is_none")] team_id: Option, #[serde(skip_serializing_if = "Option::is_none")] signature: Option, #[serde(skip_serializing_if = "Option::is_none")] preflight: Option, #[serde(skip_serializing_if = "Option::is_none")] timeout: Option, #[serde(skip_serializing_if = "Option::is_none")] enabled: Option, } impl From<&RawRemoteCacheOptions> for ConfigurationOptions { fn from(remote_cache_opts: &RawRemoteCacheOptions) -> Self { Self { api_url: remote_cache_opts.api_url.clone(), login_url: remote_cache_opts.login_url.clone(), team_slug: remote_cache_opts.team_slug.clone(), team_id: remote_cache_opts.team_id.clone(), signature: remote_cache_opts.signature, preflight: remote_cache_opts.preflight, timeout: remote_cache_opts.timeout, enabled: remote_cache_opts.enabled, ..Self::default() } } } #[derive(Serialize, Default, Debug, Clone, Iterable, Deserializable)] #[serde(rename_all = "camelCase")] // The raw deserialized turbo.json file. pub struct RawTurboJson { #[serde(skip)] span: Spanned<()>, #[serde(rename = "$schema", skip_serializing_if = "Option::is_none")] schema: Option, #[serde(skip_serializing_if = "Option::is_none")] pub experimental_spaces: Option, #[serde(skip_serializing_if = "Option::is_none")] extends: Option>>, // Global root filesystem dependencies #[serde(skip_serializing_if = "Option::is_none")] global_dependencies: Option>>, #[serde(skip_serializing_if = "Option::is_none")] global_env: Option>>, #[serde(skip_serializing_if = "Option::is_none")] global_pass_through_env: Option>>, // Tasks is a map of task entries which define the task graph // and cache behavior on a per task or per package-task basis. #[serde(skip_serializing_if = "Option::is_none")] pub tasks: Option, #[serde(skip_serializing)] pub pipeline: Option>, // Configuration options when interfacing with the remote cache #[serde(skip_serializing_if = "Option::is_none")] pub(crate) remote_cache: Option, #[serde(skip_serializing_if = "Option::is_none", rename = "ui")] pub ui: Option, #[serde( skip_serializing_if = "Option::is_none", rename = "dangerouslyDisablePackageManagerCheck" )] pub allow_no_package_manager: Option, #[serde(skip_serializing_if = "Option::is_none")] pub daemon: Option>, #[serde(skip_serializing_if = "Option::is_none")] pub env_mode: Option, #[serde(skip_serializing_if = "Option::is_none")] pub cache_dir: Option>, #[deserializable(rename = "//")] #[serde(skip)] _comment: Option, } #[derive(Serialize, Default, Debug, PartialEq, Clone)] #[serde(transparent)] pub struct Pipeline(BTreeMap, Spanned>); impl IntoIterator for Pipeline { type Item = (TaskName<'static>, Spanned); type IntoIter = , Spanned> as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } } impl Deref for Pipeline { type Target = BTreeMap, Spanned>; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for Pipeline { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } #[derive(Serialize, Deserialize, Debug, Copy, Clone, Deserializable, PartialEq, Eq, ValueEnum)] #[serde(rename_all = "camelCase")] pub enum UIMode { /// Use the terminal user interface Tui, /// Use the standard output stream Stream, /// Use the web user interface (experimental) Web, } impl Default for UIMode { fn default() -> Self { Self::Tui } } impl UIMode { pub fn use_tui(&self) -> bool { matches!(self, Self::Tui) } /// Returns true if the UI mode has a sender, /// i.e. web or tui but not stream pub fn has_sender(&self) -> bool { matches!(self, Self::Tui | Self::Web) } } #[derive(Serialize, Default, Debug, PartialEq, Clone, Iterable, Deserializable)] #[serde(rename_all = "camelCase")] #[deserializable(unknown_fields = "deny")] pub struct RawTaskDefinition { #[serde(skip_serializing_if = "Option::is_none")] cache: Option>, #[serde(skip_serializing_if = "Option::is_none")] depends_on: Option>>>, #[serde(skip_serializing_if = "Option::is_none")] env: Option>>, #[serde(skip_serializing_if = "Option::is_none")] inputs: Option>>, #[serde(skip_serializing_if = "Option::is_none")] pass_through_env: Option>>, #[serde(skip_serializing_if = "Option::is_none")] persistent: Option>, #[serde(skip_serializing_if = "Option::is_none")] interruptible: Option>, #[serde(skip_serializing_if = "Option::is_none")] outputs: Option>>, #[serde(skip_serializing_if = "Option::is_none")] output_logs: Option>, #[serde(skip_serializing_if = "Option::is_none")] interactive: Option>, // TODO: Remove this once we have the ability to load task definitions directly // instead of deriving them from a TurboJson #[serde(skip)] env_mode: Option, } macro_rules! set_field { ($this:ident, $other:ident, $field:ident) => {{ if let Some(field) = $other.$field { $this.$field = field.into(); } }}; } impl RawTaskDefinition { // merge accepts a RawTaskDefinition and // merges it into RawTaskDefinition. pub fn merge(&mut self, other: RawTaskDefinition) { set_field!(self, other, outputs); let other_has_range = other.cache.as_ref().map_or(false, |c| c.range.is_some()); let self_does_not_have_range = self.cache.as_ref().map_or(false, |c| c.range.is_none()); if other.cache.is_some() // If other has range info and we're missing it, carry it over || (other_has_range && self_does_not_have_range) { self.cache = other.cache; } set_field!(self, other, depends_on); set_field!(self, other, inputs); set_field!(self, other, output_logs); set_field!(self, other, persistent); set_field!(self, other, interruptible); set_field!(self, other, env); set_field!(self, other, pass_through_env); set_field!(self, other, interactive); set_field!(self, other, env_mode); } } pub const CONFIG_FILE: &str = "turbo.json"; const ENV_PIPELINE_DELIMITER: &str = "$"; const TOPOLOGICAL_PIPELINE_DELIMITER: &str = "^"; impl TryFrom>> for TaskOutputs { type Error = Error; fn try_from(outputs: Vec>) -> Result { let mut inclusions = Vec::new(); let mut exclusions = Vec::new(); for glob in outputs { if let Some(stripped_glob) = glob.value.strip_prefix('!') { if Utf8Path::new(stripped_glob).is_absolute() { let (span, text) = glob.span_and_text("turbo.json"); return Err(Error::AbsolutePathInConfig { field: "outputs", span, text, }); } exclusions.push(stripped_glob.to_string()); } else { if Utf8Path::new(&glob.value).is_absolute() { let (span, text) = glob.span_and_text("turbo.json"); return Err(Error::AbsolutePathInConfig { field: "outputs", span, text, }); } inclusions.push(glob.into_inner().into()); } } inclusions.sort(); exclusions.sort(); Ok(TaskOutputs { inclusions, exclusions, }) } } impl TryFrom for TaskDefinition { type Error = Error; fn try_from(raw_task: RawTaskDefinition) -> Result { let outputs = raw_task.outputs.unwrap_or_default().try_into()?; let cache = raw_task.cache.map_or(true, |c| c.into_inner()); let interactive = raw_task .interactive .as_ref() .map(|value| value.value) .unwrap_or_default(); if let Some(interactive) = raw_task.interactive { let (span, text) = interactive.span_and_text("turbo.json"); if cache && interactive.value { return Err(Error::InteractiveNoCacheable { span, text }); } } let persistent = *raw_task.persistent.unwrap_or_default(); let interruptible = raw_task.interruptible.unwrap_or_default(); if *interruptible && !persistent { let (span, text) = interruptible.span_and_text("turbo.json"); return Err(Error::InterruptibleButNotPersistent { span, text }); } let mut env_var_dependencies = HashSet::new(); let mut topological_dependencies: Vec> = Vec::new(); let mut task_dependencies: Vec> = Vec::new(); if let Some(depends_on) = raw_task.depends_on { for dependency in depends_on.into_inner() { let (span, text) = dependency.span_and_text("turbo.json"); let (dependency, depspan) = dependency.split(); let dependency: String = dependency.into(); if dependency.strip_prefix(ENV_PIPELINE_DELIMITER).is_some() { return Err(Error::InvalidDependsOnValue { field: "dependsOn", span, text, }); } else if let Some(topo_dependency) = dependency.strip_prefix(TOPOLOGICAL_PIPELINE_DELIMITER) { topological_dependencies.push(depspan.to(topo_dependency.to_string().into())); } else { task_dependencies.push(depspan.to(dependency.into())); } } } task_dependencies.sort_by(|a, b| a.value.cmp(&b.value)); topological_dependencies.sort_by(|a, b| a.value.cmp(&b.value)); let env = raw_task .env .map(|env| -> Result, Error> { gather_env_vars(env, "env", &mut env_var_dependencies)?; let mut env_var_dependencies: Vec = env_var_dependencies.into_iter().collect(); env_var_dependencies.sort(); Ok(env_var_dependencies) }) .transpose()? .unwrap_or_default(); let inputs = raw_task .inputs .unwrap_or_default() .into_iter() .map(|input| { if Utf8Path::new(&input.value).is_absolute() { let (span, text) = input.span_and_text("turbo.json"); Err(Error::AbsolutePathInConfig { field: "inputs", span, text, }) } else { Ok(input.to_string()) } }) .collect::, _>>()?; let pass_through_env = raw_task .pass_through_env .map(|env| -> Result, Error> { let mut pass_through_env = HashSet::new(); gather_env_vars(env, "passThroughEnv", &mut pass_through_env)?; let mut pass_through_env: Vec = pass_through_env.into_iter().collect(); pass_through_env.sort(); Ok(pass_through_env) }) .transpose()?; Ok(TaskDefinition { outputs, cache, topological_dependencies, task_dependencies, env, inputs, pass_through_env, output_logs: *raw_task.output_logs.unwrap_or_default(), persistent, interruptible: *interruptible, interactive, env_mode: raw_task.env_mode, }) } } impl RawTurboJson { pub(crate) fn read( repo_root: &AbsoluteSystemPath, path: &AbsoluteSystemPath, ) -> Result { let contents = path.read_to_string()?; // Anchoring the path can fail if the path resides outside of the repository // Just display absolute path in that case. let root_relative_path = repo_root.anchor(path).map_or_else( |_| path.as_str().to_owned(), |relative| relative.to_string(), ); let raw_turbo_json = RawTurboJson::parse(&contents, &root_relative_path)?; Ok(raw_turbo_json) } /// Produces a new turbo.json without any tasks that reference non-existent /// workspaces pub fn prune_tasks>(&self, workspaces: &[S]) -> Self { let mut this = self.clone(); if let Some(pipeline) = &mut this.tasks { pipeline.0.retain(|task_name, _| { task_name.in_workspace(ROOT_PKG_NAME) || workspaces .iter() .any(|workspace| task_name.in_workspace(workspace.as_ref())) }) } this } pub fn from_task_access_trace(trace: &HashMap) -> Option { if trace.is_empty() { return None; } let mut pipeline = Pipeline::default(); for (task_name, trace_file) in trace { let spanned_outputs: Vec> = trace_file .outputs .iter() .map(|output| Spanned::new(output.clone())) .collect(); let task_definition = RawTaskDefinition { outputs: Some(spanned_outputs), env: Some( trace_file .accessed .env_var_keys .iter() .map(|unescaped_string| Spanned::new(unescaped_string.clone())) .collect(), ), ..Default::default() }; let name = TaskName::from(task_name.as_str()); let root_task = name.into_root_task(); pipeline.insert(root_task, Spanned::new(task_definition.clone())); } Some(RawTurboJson { tasks: Some(pipeline), ..RawTurboJson::default() }) } } impl TryFrom for TurboJson { type Error = Error; fn try_from(raw_turbo: RawTurboJson) -> Result { if let Some(pipeline) = raw_turbo.pipeline { let (span, text) = pipeline.span_and_text("turbo.json"); return Err(Error::PipelineField { span, text }); } let mut global_env = HashSet::new(); let mut global_file_dependencies = HashSet::new(); if let Some(global_env_from_turbo) = raw_turbo.global_env { gather_env_vars(global_env_from_turbo, "globalEnv", &mut global_env)?; } for global_dep in raw_turbo.global_dependencies.into_iter().flatten() { if global_dep.strip_prefix(ENV_PIPELINE_DELIMITER).is_some() { let (span, text) = global_dep.span_and_text("turbo.json"); return Err(Error::InvalidDependsOnValue { field: "globalDependencies", span, text, }); } else if Utf8Path::new(&global_dep.value).is_absolute() { let (span, text) = global_dep.span_and_text("turbo.json"); return Err(Error::AbsolutePathInConfig { field: "globalDependencies", span, text, }); } else { global_file_dependencies.insert(global_dep.into_inner().into()); } } Ok(TurboJson { text: raw_turbo.span.text, path: raw_turbo.span.path, global_env: { let mut global_env: Vec<_> = global_env.into_iter().collect(); global_env.sort(); global_env }, global_pass_through_env: raw_turbo .global_pass_through_env .map(|env| -> Result, Error> { let mut global_pass_through_env = HashSet::new(); gather_env_vars(env, "globalPassThroughEnv", &mut global_pass_through_env)?; let mut global_pass_through_env: Vec = global_pass_through_env.into_iter().collect(); global_pass_through_env.sort(); Ok(global_pass_through_env) }) .transpose()?, global_deps: { let mut global_deps: Vec<_> = global_file_dependencies.into_iter().collect(); global_deps.sort(); global_deps }, tasks: raw_turbo.tasks.unwrap_or_default(), // copy these over, we don't need any changes here. extends: raw_turbo .extends .unwrap_or_default() .map(|s| s.into_iter().map(|s| s.into()).collect()), // Spaces and Remote Cache config is handled through layered config }) } } impl TurboJson { fn has_task(&self, task_name: &TaskName) -> bool { for key in self.tasks.keys() { if key == task_name || (key.task() == task_name.task() && !task_name.is_package_task()) { return true; } } false } /// Reads a `RawTurboJson` from the given path /// and then converts it into `TurboJson` pub(crate) fn read( repo_root: &AbsoluteSystemPath, path: &AbsoluteSystemPath, ) -> Result { let raw_turbo_json = RawTurboJson::read(repo_root, path)?; raw_turbo_json.try_into() } pub fn task(&self, task_id: &TaskId, task_name: &TaskName) -> Option { match self.tasks.get(&task_id.as_task_name()) { Some(entry) => Some(entry.value.clone()), None => self.tasks.get(task_name).map(|entry| entry.value.clone()), } } pub fn validate(&self, validations: &[TurboJSONValidation]) -> Vec { validations .iter() .flat_map(|validation| validation(self)) .collect() } pub fn has_root_tasks(&self) -> bool { self.tasks .iter() .any(|(task_name, _)| task_name.package() == Some(ROOT_PKG_NAME)) } /// Adds a local proxy task to a workspace TurboJson pub fn with_proxy(&mut self, needs_proxy_build: bool) { if self.extends.is_empty() { self.extends = Spanned::new(vec!["//".into()]); } self.tasks.insert( TaskName::from("proxy"), Spanned::new(RawTaskDefinition { cache: Some(Spanned::new(false)), depends_on: needs_proxy_build.then(|| { Spanned::new(vec![Spanned::new(UnescapedString::from(format!( "{MICRO_FRONTENDS_PACKAGE_INTERNAL}#build" )))]) }), ..Default::default() }), ); } } type TurboJSONValidation = fn(&TurboJson) -> Vec; pub fn validate_no_package_task_syntax(turbo_json: &TurboJson) -> Vec { turbo_json .tasks .iter() .filter(|(task_name, _)| task_name.is_package_task()) .map(|(task_name, entry)| { let (span, text) = entry.span_and_text("turbo.json"); Error::UnnecessaryPackageTaskSyntax { actual: task_name.to_string(), wanted: task_name.task().to_string(), span, text, } }) .collect() } pub fn validate_extends(turbo_json: &TurboJson) -> Vec { match turbo_json.extends.first() { Some(package_name) if package_name != ROOT_PKG_NAME || turbo_json.extends.len() > 1 => { let (span, text) = turbo_json.extends.span_and_text("turbo.json"); vec![Error::ExtendFromNonRoot { span, text }] } None => { let path = turbo_json .path .as_ref() .map_or("turbo.json", |p| p.as_ref()); let (span, text) = match turbo_json.text { Some(ref text) => { let len = text.len(); let span: SourceSpan = (0, len - 1).into(); (Some(span), text.to_string()) } None => (None, String::new()), }; vec![Error::NoExtends { span, text: NamedSource::new(path, text), }] } _ => vec![], } } fn gather_env_vars( vars: Vec>>, key: &str, into: &mut HashSet, ) -> Result<(), Error> { for value in vars { let value: Spanned = value.map(|v| v.into()); if value.starts_with(ENV_PIPELINE_DELIMITER) { let (span, text) = value.span_and_text("turbo.json"); // Hard error to help people specify this correctly during migration. // TODO: Remove this error after we have run summary. let path = value .path .as_ref() .map_or_else(|| "turbo.json".to_string(), |p| p.to_string()); return Err(Error::InvalidEnvPrefix(Box::new(InvalidEnvPrefixError { key: key.to_string(), value: value.into_inner(), span, text: NamedSource::new(path, text), env_pipeline_delimiter: ENV_PIPELINE_DELIMITER, }))); } into.insert(value.into_inner()); } Ok(()) } #[cfg(test)] mod tests { use anyhow::Result; use biome_deserialize::json::deserialize_from_json_str; use biome_json_parser::JsonParserOptions; use pretty_assertions::assert_eq; use serde_json::json; use test_case::test_case; use turborepo_unescape::UnescapedString; use super::{RawTurboJson, Spanned, UIMode}; use crate::{ cli::OutputLogsMode, run::task_id::TaskName, task_graph::{TaskDefinition, TaskOutputs}, turbo_json::RawTaskDefinition, }; #[test_case( "{}", RawTaskDefinition::default(), TaskDefinition::default() ; "empty")] #[test_case( r#"{ "persistent": false }"#, RawTaskDefinition { persistent: Some(Spanned::new(false).with_range(16..21)), ..RawTaskDefinition::default() }, TaskDefinition::default() ; "just persistent" )] #[test_case( r#"{ "dependsOn": ["cli#build"], "env": ["OS"], "passThroughEnv": ["AWS_SECRET_KEY"], "outputs": ["package/a/dist"], "cache": false, "inputs": ["package/a/src/**"], "outputLogs": "full", "persistent": true, "interactive": true, "interruptible": true }"#, RawTaskDefinition { depends_on: Some(Spanned::new(vec![Spanned::::new("cli#build".into()).with_range(26..37)]).with_range(25..38)), env: Some(vec![Spanned::::new("OS".into()).with_range(58..62)]), pass_through_env: Some(vec![Spanned::::new("AWS_SECRET_KEY".into()).with_range(94..110)]), outputs: Some(vec![Spanned::::new("package/a/dist".into()).with_range(135..151)]), cache: Some(Spanned::new(false).with_range(173..178)), inputs: Some(vec![Spanned::::new("package/a/src/**".into()).with_range(201..219)]), output_logs: Some(Spanned::new(OutputLogsMode::Full).with_range(246..252)), persistent: Some(Spanned::new(true).with_range(278..282)), interactive: Some(Spanned::new(true).with_range(309..313)), interruptible: Some(Spanned::new(true).with_range(342..346)), env_mode: None, }, TaskDefinition { env: vec!["OS".to_string()], outputs: TaskOutputs { inclusions: vec!["package/a/dist".to_string()], exclusions: vec![], }, cache: false, inputs: vec!["package/a/src/**".to_string()], output_logs: OutputLogsMode::Full, pass_through_env: Some(vec!["AWS_SECRET_KEY".to_string()]), task_dependencies: vec![Spanned::>::new("cli#build".into()).with_range(26..37)], topological_dependencies: vec![], persistent: true, interactive: true, interruptible: true, env_mode: None, } ; "full" )] #[test_case( r#"{ "dependsOn": ["cli#build"], "env": ["OS"], "passThroughEnv": ["AWS_SECRET_KEY"], "outputs": ["package\\a\\dist"], "cache": false, "inputs": ["package\\a\\src\\**"], "outputLogs": "full", "persistent": true, "interruptible": true }"#, RawTaskDefinition { depends_on: Some(Spanned::new(vec![Spanned::::new("cli#build".into()).with_range(30..41)]).with_range(29..42)), env: Some(vec![Spanned::::new("OS".into()).with_range(66..70)]), pass_through_env: Some(vec![Spanned::::new("AWS_SECRET_KEY".into()).with_range(106..122)]), outputs: Some(vec![Spanned::::new("package\\a\\dist".into()).with_range(151..169)]), cache: Some(Spanned::new(false).with_range(195..200)), inputs: Some(vec![Spanned::::new("package\\a\\src\\**".into()).with_range(227..248)]), output_logs: Some(Spanned::new(OutputLogsMode::Full).with_range(279..285)), persistent: Some(Spanned::new(true).with_range(315..319)), interruptible: Some(Spanned::new(true).with_range(352..356)), interactive: None, env_mode: None, }, TaskDefinition { env: vec!["OS".to_string()], outputs: TaskOutputs { inclusions: vec!["package\\a\\dist".to_string()], exclusions: vec![], }, cache: false, inputs: vec!["package\\a\\src\\**".to_string()], output_logs: OutputLogsMode::Full, pass_through_env: Some(vec!["AWS_SECRET_KEY".to_string()]), task_dependencies: vec![Spanned::>::new("cli#build".into()).with_range(30..41)], topological_dependencies: vec![], persistent: true, interruptible: true, interactive: false, env_mode: None, } ; "full (windows)" )] fn test_deserialize_task_definition( task_definition_content: &str, expected_raw_task_definition: RawTaskDefinition, expected_task_definition: TaskDefinition, ) -> Result<()> { let deserialized_result = deserialize_from_json_str( task_definition_content, JsonParserOptions::default().with_allow_comments(), "turbo.json", ); let raw_task_definition: RawTaskDefinition = deserialized_result.into_deserialized().unwrap(); assert_eq!(raw_task_definition, expected_raw_task_definition); let task_definition: TaskDefinition = raw_task_definition.try_into()?; assert_eq!(task_definition, expected_task_definition); Ok(()) } #[test_case("[]", TaskOutputs::default() ; "empty")] #[test_case(r#"["target/**"]"#, TaskOutputs { inclusions: vec!["target/**".to_string()], exclusions: vec![] })] #[test_case( r#"[".next/**", "!.next/cache/**"]"#, TaskOutputs { inclusions: vec![".next/**".to_string()], exclusions: vec![".next/cache/**".to_string()] } ; "with .next" )] #[test_case( r#"[".next\\**", "!.next\\cache\\**"]"#, TaskOutputs { inclusions: vec![".next\\**".to_string()], exclusions: vec![".next\\cache\\**".to_string()] } ; "with .next (windows)" )] fn test_deserialize_task_outputs( task_outputs_str: &str, expected_task_outputs: TaskOutputs, ) -> Result<()> { let raw_task_outputs: Vec = serde_json::from_str(task_outputs_str)?; let raw_task_outputs = raw_task_outputs .into_iter() .map(Spanned::new) .collect::>(); let task_outputs: TaskOutputs = raw_task_outputs.try_into()?; assert_eq!(task_outputs, expected_task_outputs); Ok(()) } #[test] fn test_turbo_task_pruning() { let json = RawTurboJson::parse_from_serde(json!({ "tasks": { "//#top": {}, "build": {}, "a#build": {}, "b#build": {}, } })) .unwrap(); let pruned_json = json.prune_tasks(&["a"]); let expected: RawTurboJson = RawTurboJson::parse_from_serde(json!({ "tasks": { "//#top": {}, "build": {}, "a#build": {}, } })) .unwrap(); // We do this comparison manually so we don't compare the `task_name_range` // fields, which are expected to be different let pruned_pipeline = pruned_json.tasks.unwrap(); let expected_pipeline = expected.tasks.unwrap(); for ( (pruned_task_name, pruned_pipeline_entry), (expected_task_name, expected_pipeline_entry), ) in pruned_pipeline .into_iter() .zip(expected_pipeline.into_iter()) { assert_eq!(pruned_task_name, expected_task_name); assert_eq!(pruned_pipeline_entry.value, expected_pipeline_entry.value); } } #[test_case("full", Some(OutputLogsMode::Full) ; "full")] #[test_case("hash-only", Some(OutputLogsMode::HashOnly) ; "hash-only")] #[test_case("new-only", Some(OutputLogsMode::NewOnly) ; "new-only")] #[test_case("errors-only", Some(OutputLogsMode::ErrorsOnly) ; "errors-only")] #[test_case("none", Some(OutputLogsMode::None) ; "none")] #[test_case("junk", None ; "invalid value")] fn test_parsing_output_logs_mode(output_logs: &str, expected: Option) { let json: Result = RawTurboJson::parse_from_serde(json!({ "tasks": { "build": { "outputLogs": output_logs, } } })); let actual = json .as_ref() .ok() .and_then(|j| j.tasks.as_ref()) .and_then(|pipeline| pipeline.0.get(&TaskName::from("build"))) .and_then(|build| build.value.output_logs.clone()) .map(|mode| mode.into_inner()); assert_eq!(actual, expected); } #[test_case(r#"{ "ui": "tui" }"#, Some(UIMode::Tui) ; "tui")] #[test_case(r#"{ "ui": "stream" }"#, Some(UIMode::Stream) ; "stream")] #[test_case(r#"{}"#, None ; "missing")] fn test_ui(json: &str, expected: Option) { let json = RawTurboJson::parse(json, "").unwrap(); assert_eq!(json.ui, expected); } #[test_case(r#"{ "daemon": true }"#, r#"{"daemon":true}"# ; "daemon_on")] #[test_case(r#"{ "daemon": false }"#, r#"{"daemon":false}"# ; "daemon_off")] fn test_daemon(json: &str, expected: &str) { let parsed = RawTurboJson::parse(json, "").unwrap(); let actual = serde_json::to_string(&parsed).unwrap(); assert_eq!(actual, expected); } #[test_case(r#"{ "ui": "tui" }"#, r#"{"ui":"tui"}"# ; "tui")] #[test_case(r#"{ "ui": "stream" }"#, r#"{"ui":"stream"}"# ; "stream")] fn test_ui_serialization(input: &str, expected: &str) { let parsed = RawTurboJson::parse(input, "").unwrap(); let actual = serde_json::to_string(&parsed).unwrap(); assert_eq!(actual, expected); } #[test_case(r#"{"dangerouslyDisablePackageManagerCheck":true}"#, Some(true) ; "t")] #[test_case(r#"{"dangerouslyDisablePackageManagerCheck":false}"#, Some(false) ; "f")] #[test_case(r#"{}"#, None ; "missing")] fn test_allow_no_package_manager_serde(json_str: &str, expected: Option) { let json = RawTurboJson::parse(json_str, "").unwrap(); assert_eq!(json.allow_no_package_manager, expected); let serialized = serde_json::to_string(&json).unwrap(); assert_eq!(serialized, json_str); } }