-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
cli.rs
1435 lines (1322 loc) · 47.3 KB
/
cli.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::{
env::{self, current_dir},
io, mem,
path::{Path, PathBuf},
process,
};
use anyhow::{anyhow, Result};
use clap::{ArgAction, CommandFactory, Parser, Subcommand, ValueEnum};
use clap_complete::{generate, Shell};
use dunce::canonicalize as fs_canonicalize;
use log::{debug, error};
use serde::Serialize;
use crate::{
commands::{bin, daemon, link, login, logout, unlink, CommandBase},
get_version,
shim::{RepoMode, RepoState},
ui::UI,
Payload,
};
// Global turbo sets this environment variable to its cwd so that local
// turbo can use it for package inference.
pub const INVOCATION_DIR_ENV_VAR: &str = "TURBO_INVOCATION_DIR";
#[derive(Copy, Clone, Debug, PartialEq, Serialize, ValueEnum)]
pub enum OutputLogsMode {
#[serde(rename = "full")]
Full,
#[serde(rename = "none")]
None,
#[serde(rename = "hash-only")]
HashOnly,
#[serde(rename = "new-only")]
NewOnly,
#[serde(rename = "errors-only")]
ErrorsOnly,
}
impl Default for OutputLogsMode {
fn default() -> Self {
Self::Full
}
}
// NOTE: These *must* be kept in sync with the `_dryRunJSONValue`
// and `_dryRunTextValue` constants in run.go.
#[derive(Copy, Clone, Debug, PartialEq, Serialize, ValueEnum)]
pub enum DryRunMode {
Text,
Json,
}
#[derive(Copy, Clone, Debug, PartialEq, Serialize, ValueEnum)]
pub enum EnvMode {
Infer,
Loose,
Strict,
}
impl Default for EnvMode {
fn default() -> EnvMode {
EnvMode::Infer
}
}
#[derive(Parser, Clone, Default, Debug, PartialEq, Serialize)]
#[clap(author, about = "The build system that makes ship happen", long_about = None)]
#[clap(disable_help_subcommand = true)]
#[clap(disable_version_flag = true)]
#[clap(arg_required_else_help = true)]
pub struct Args {
#[clap(long, global = true)]
pub version: bool,
#[clap(long, global = true)]
#[serde(skip)]
/// Skip any attempts to infer which version of Turbo the project is
/// configured to use
pub skip_infer: bool,
/// Disable the turbo update notification
#[clap(long, global = true)]
pub no_update_notifier: bool,
/// Override the endpoint for API calls
#[clap(long, global = true, value_parser)]
pub api: Option<String>,
/// Force color usage in the terminal
#[clap(long, global = true)]
pub color: bool,
/// Specify a file to save a cpu profile
#[clap(long = "cpuprofile", global = true, value_parser)]
pub cpu_profile: Option<String>,
/// The directory in which to run turbo
#[clap(long, global = true, value_parser)]
pub cwd: Option<PathBuf>,
/// Specify a file to save a pprof heap profile
#[clap(long, global = true, value_parser)]
pub heap: Option<String>,
/// Override the login endpoint
#[clap(long, global = true, value_parser)]
pub login: Option<String>,
/// Suppress color usage in the terminal
#[clap(long, global = true)]
pub no_color: bool,
/// When enabled, turbo will precede HTTP requests with an OPTIONS request
/// for authorization
#[clap(long, global = true)]
pub preflight: bool,
/// Set a timeout for all HTTP requests.
#[clap(long, value_name = "TIMEOUT", global = true, value_parser)]
pub remote_cache_timeout: Option<u64>,
/// Set the team slug for API calls
#[clap(long, global = true, value_parser)]
pub team: Option<String>,
/// Set the auth token for API calls
#[clap(long, global = true, value_parser)]
pub token: Option<String>,
/// Specify a file to save a pprof trace
#[clap(long, global = true, value_parser)]
pub trace: Option<String>,
/// verbosity
#[clap(flatten)]
pub verbosity: Verbosity,
#[clap(long, global = true, hide = true)]
/// Force a check for a new version of turbo
pub check_for_update: bool,
#[clap(long = "__test-run", global = true, hide = true)]
pub test_run: bool,
#[clap(flatten, next_help_heading = "Run Arguments")]
pub run_args: Option<RunArgs>,
#[clap(subcommand)]
pub command: Option<Command>,
}
#[derive(Debug, Parser, Serialize, Clone, Copy, PartialEq, Eq, Default)]
#[serde(into = "u8")]
pub struct Verbosity {
#[clap(
long = "verbosity",
global = true,
conflicts_with = "v",
value_name = "COUNT"
)]
/// Verbosity level
pub verbosity: Option<u8>,
#[clap(
short = 'v',
action = clap::ArgAction::Count,
global = true,
hide = true,
conflicts_with = "verbosity"
)]
pub v: u8,
}
impl From<Verbosity> for u8 {
fn from(val: Verbosity) -> Self {
let Verbosity { verbosity, v } = val;
verbosity.unwrap_or(v)
}
}
#[derive(Subcommand, Copy, Clone, Debug, Serialize, PartialEq)]
#[serde(tag = "command")]
pub enum DaemonCommand {
/// Restarts the turbo daemon
Restart,
/// Ensures that the turbo daemon is running
Start,
/// Reports the status of the turbo daemon
Status {
/// Pass --json to report status in JSON format
#[clap(long)]
json: bool,
},
/// Stops the turbo daemon
Stop,
}
#[derive(Copy, Clone, Debug, PartialEq, Serialize, ValueEnum)]
pub enum LinkTarget {
RemoteCache,
Spaces,
}
impl Args {
pub fn new() -> Result<Self> {
let mut clap_args = match Args::try_parse() {
Ok(args) => args,
// Don't use error logger when displaying help text
Err(e)
if matches!(
e.kind(),
clap::error::ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
) =>
{
let _ = e.print();
process::exit(1);
}
Err(e) if e.use_stderr() => {
let err_str = e.to_string();
// A cleaner solution would be to implement our own clap::error::ErrorFormatter
// but that would require copying the default formatter just to remove this
// line: https://docs.rs/clap/latest/src/clap/error/format.rs.html#100
error!(
"{}",
err_str.strip_prefix("error: ").unwrap_or(err_str.as_str())
);
process::exit(1);
}
// If the clap error shouldn't be printed to stderr it indicates help text
Err(e) => {
let _ = e.print();
process::exit(0);
}
};
// --version flag doesn't work with ignore_errors in clap, so we have to handle
// it manually
if clap_args.version {
println!("{}", get_version());
process::exit(0);
}
if env::var("TEST_RUN").is_ok() {
clap_args.test_run = true;
}
Ok(clap_args)
}
}
/// Defines the subcommands for CLI. NOTE: If we change the commands in Go,
/// we must change these as well to avoid accidentally passing the
/// --single-package flag into non-build commands.
#[derive(Subcommand, Clone, Debug, Serialize, PartialEq)]
pub enum Command {
// NOTE: Empty variants still have an empty struct attached so that serde serializes
// them as `{ "Bin": {} }` instead of as `"Bin"`.
/// Get the path to the Turbo binary
Bin {},
/// Generate the autocompletion script for the specified shell
#[serde(skip)]
Completion { shell: Shell },
/// Runs the Turborepo background daemon
Daemon {
/// Set the idle timeout for turbod (default 4h0m0s)
#[clap(long)]
idle_time: Option<String>,
#[clap(subcommand)]
#[serde(flatten)]
command: Option<DaemonCommand>,
},
/// Link your local directory to a Vercel organization and enable remote
/// caching.
Link {
/// Do not create or modify .gitignore (default false)
#[clap(long)]
no_gitignore: bool,
/// Specify what should be linked (default "remote cache")
#[clap(long, value_enum, default_value_t = LinkTarget::RemoteCache)]
target: LinkTarget,
},
/// Login to your Vercel account
Login {
#[clap(long = "sso-team")]
sso_team: Option<String>,
},
/// Logout to your Vercel account
Logout {},
/// Prepare a subset of your monorepo.
Prune {
#[clap(long)]
scope: Vec<String>,
#[clap(long)]
docker: bool,
#[clap(long = "out-dir", default_value_t = String::from("out"), value_parser)]
output_dir: String,
},
/// Run tasks across projects in your monorepo
///
/// By default, turbo executes tasks in topological order (i.e.
/// dependencies first) and then caches the results. Re-running commands for
/// tasks already in the cache will skip re-execution and immediately move
/// artifacts from the cache into the correct output folders (as if the task
/// occurred again).
///
/// Arguments passed after '--' will be passed through to the named tasks.
Run(Box<RunArgs>),
/// Unlink the current directory from your Vercel organization and disable
/// Remote Caching
Unlink {
/// Specify what should be unlinked (default "remote cache")
#[clap(long, value_enum, default_value_t = LinkTarget::RemoteCache)]
target: LinkTarget,
},
}
#[derive(Parser, Clone, Debug, Default, Serialize, PartialEq)]
pub struct RunArgs {
/// Override the filesystem cache directory.
#[clap(long)]
pub cache_dir: Option<String>,
/// Set the number of concurrent cache operations (default 10)
#[clap(long, default_value_t = 10)]
pub cache_workers: u32,
/// Limit the concurrency of task execution. Use 1 for serial (i.e.
/// one-at-a-time) execution.
#[clap(long)]
pub concurrency: Option<String>,
/// Continue execution even if a task exits with an error or non-zero
/// exit code. The default behavior is to bail
#[clap(long = "continue")]
pub continue_execution: bool,
#[clap(alias = "dry", long = "dry-run", num_args = 0..=1, default_missing_value = "text")]
pub dry_run: Option<DryRunMode>,
/// Run turbo in single-package mode
#[clap(long, global = true)]
pub single_package: bool,
/// Use the given selector to specify package(s) to act as
/// entry points. The syntax mirrors pnpm's syntax, and
/// additional documentation and examples can be found in
/// turbo's documentation https://turbo.build/repo/docs/reference/command-line-reference#--filter
#[clap(short = 'F', long, action = ArgAction::Append)]
pub filter: Vec<String>,
/// Ignore the existing cache (to force execution)
#[clap(long)]
pub force: bool,
/// Specify glob of global filesystem dependencies to be hashed. Useful
/// for .env and files
#[clap(long = "global-deps", action = ArgAction::Append)]
pub global_deps: Vec<String>,
/// Generate a graph of the task execution and output to a file when a
/// filename is specified (.svg, .png, .jpg, .pdf, .json,
/// .html). Outputs dot graph to stdout when if no filename is provided
#[clap(long, num_args = 0..=1, default_missing_value = "")]
pub graph: Option<String>,
/// Environment variable mode.
/// Loose passes the entire environment.
/// Strict uses an allowlist specified in turbo.json.
#[clap(long = "experimental-env-mode", default_value = "infer", num_args = 0..=1, default_missing_value = "infer", hide = true)]
pub env_mode: EnvMode,
/// Files to ignore when calculating changed files (i.e. --since).
/// Supports globs.
#[clap(long)]
pub ignore: Vec<String>,
/// Include the dependencies of tasks in execution.
#[clap(long)]
pub include_dependencies: bool,
/// Avoid saving task results to the cache. Useful for development/watch
/// tasks.
#[clap(long)]
pub no_cache: bool,
/// Run without using turbo's daemon process
#[clap(long)]
pub no_daemon: bool,
/// Exclude dependent task consumers from execution.
#[clap(long)]
pub no_deps: bool,
/// Set type of process output logging. Use "full" to show
/// all output. Use "hash-only" to show only turbo-computed
/// task hashes. Use "new-only" to show only new output with
/// only hashes for cached tasks. Use "none" to hide process
/// output. (default full)
#[clap(long, value_enum)]
pub output_logs: Option<OutputLogsMode>,
#[clap(long, hide = true)]
pub only: bool,
/// Execute all tasks in parallel.
#[clap(long)]
pub parallel: bool,
#[clap(long, hide = true, default_missing_value = "")]
pub pkg_inference_root: Option<String>,
/// File to write turbo's performance profile output into.
/// You can load the file up in chrome://tracing to see
/// which parts of your build were slow.
#[clap(long)]
pub profile: Option<String>,
/// Ignore the local filesystem cache for all tasks. Only
/// allow reading and caching artifacts using the remote cache.
#[clap(long)]
pub remote_only: bool,
/// Specify package(s) to act as entry points for task execution.
/// Supports globs.
#[clap(long)]
pub scope: Vec<String>,
/// Limit/Set scope to changed packages since a mergebase.
/// This uses the git diff ${target_branch}... mechanism
/// to identify which packages have changed.
#[clap(long)]
pub since: Option<String>,
/// Generate a summary of the turbo run
#[clap(long, env = "TURBO_RUN_SUMMARY", default_missing_value = "true")]
pub summarize: Option<Option<bool>>,
/// Use "none" to remove prefixes from task logs. Note that tasks running
/// in parallel interleave their logs and prefix is the only way
/// to identify which task produced a log.
#[clap(long, value_enum)]
pub log_prefix: Option<LogPrefix>,
// NOTE: The following two are hidden because clap displays them in the help text incorrectly:
// > Usage: turbo [OPTIONS] [TASKS]... [-- <FORWARDED_ARGS>...] [COMMAND]
#[clap(hide = true)]
pub tasks: Vec<String>,
#[clap(last = true, hide = true)]
pub pass_through_args: Vec<String>,
// Pass a string to enable posting Run Summaries to Vercel
#[clap(long, hide = true)]
pub experimental_space_id: Option<String>,
}
#[derive(clap::ValueEnum, Clone, Copy, Debug, PartialEq, Serialize)]
pub enum LogPrefix {
#[serde(rename = "none")]
None,
}
/// Runs the CLI by parsing arguments with clap, then either calling Rust code
/// directly or returning a payload for the Go code to use.
///
/// Scenarios:
/// 1. inference failed, we're running this global turbo. no repo state
/// 2. --skip-infer was passed, assume we're local turbo and run. no repo state
/// 3. There is no local turbo, we're running the global one. repo state exists
/// 4. turbo binary path is set, and it's this one. repo state exists
///
/// # Arguments
///
/// * `repo_state`: If we have done repository inference and NOT executed
/// local turbo, such as in the case where `TURBO_BINARY_PATH` is set,
/// we use it here to modify clap's arguments.
///
/// returns: Result<Payload, Error>
#[tokio::main]
pub async fn run(repo_state: Option<RepoState>) -> Result<Payload> {
let mut clap_args = Args::new()?;
// If there is no command, we set the command to `Command::Run` with
// `self.parsed_args.run_args` as arguments.
if clap_args.command.is_none() {
if let Some(run_args) = mem::take(&mut clap_args.run_args) {
clap_args.command = Some(Command::Run(Box::new(run_args)));
} else {
return Err(anyhow!("No command specified"));
}
};
// If this is a run command, and we know the actual invocation path, set the
// inference root, as long as the user hasn't overridden the cwd
if clap_args.cwd.is_none() {
if let Some(Command::Run(run_args)) = &mut clap_args.command {
if let Ok(invocation_dir) = env::var(INVOCATION_DIR_ENV_VAR) {
let invocation_path = Path::new(&invocation_dir);
// If repo state doesn't exist, we're either local turbo running at the root
// (current_dir), or inference failed If repo state does exist,
// we're global turbo, and want to calculate package inference based on the repo
// root
let this_dir = current_dir()?;
let repo_root = repo_state.as_ref().map(|r| &r.root).unwrap_or(&this_dir);
if let Ok(relative_path) = invocation_path.strip_prefix(repo_root) {
debug!("pkg_inference_root set to \"{}\"", relative_path.display());
let utf8_path = relative_path
.to_str()
.ok_or_else(|| anyhow!("invalid utf8 path: {:?}", relative_path))?;
run_args.pkg_inference_root = Some(utf8_path.to_owned());
}
} else {
debug!("{} not set", INVOCATION_DIR_ENV_VAR);
}
}
}
// Do this after the above, since we're now always setting cwd.
if let Some(repo_state) = repo_state {
if let Some(Command::Run(run_args)) = &mut clap_args.command {
run_args.single_package = matches!(repo_state.mode, RepoMode::SinglePackage);
}
clap_args.cwd = Some(repo_state.root);
}
let repo_root = if let Some(cwd) = &clap_args.cwd {
let canonical_cwd = fs_canonicalize(cwd)?;
// Update on clap_args so that Go gets a canonical path.
clap_args.cwd = Some(canonical_cwd.clone());
canonical_cwd
} else {
current_dir()?
};
let version = get_version();
match clap_args.command.as_ref().unwrap() {
Command::Bin { .. } => {
bin::run()?;
Ok(Payload::Rust(Ok(0)))
}
Command::Logout { .. } => {
let mut base = CommandBase::new(clap_args, repo_root, version)?;
logout::logout(&mut base)?;
Ok(Payload::Rust(Ok(0)))
}
Command::Login { sso_team } => {
if clap_args.test_run {
println!("Login test run successful");
return Ok(Payload::Rust(Ok(0)));
}
let sso_team = sso_team.clone();
let mut base = CommandBase::new(clap_args, repo_root, version)?;
if let Some(sso_team) = sso_team {
login::sso_login(&mut base, &sso_team).await?;
} else {
login::login(&mut base).await?;
}
Ok(Payload::Rust(Ok(0)))
}
Command::Link { no_gitignore, target} => {
if clap_args.test_run {
println!("Link test run successful");
return Ok(Payload::Rust(Ok(0)));
}
let modify_gitignore = !*no_gitignore;
let to = *target;
let mut base = CommandBase::new(clap_args, repo_root, version)?;
if let Err(err) = link::link(&mut base, modify_gitignore, to).await {
error!("error: {}", err.to_string())
};
Ok(Payload::Rust(Ok(0)))
}
Command::Unlink { target } => {
if clap_args.test_run {
println!("Unlink test run successful");
return Ok(Payload::Rust(Ok(0)));
}
let from = *target;
let mut base = CommandBase::new(clap_args, repo_root, version)?;
unlink::unlink(&mut base, from)?;
Ok(Payload::Rust(Ok(0)))
}
Command::Daemon {
command: Some(command),
..
} => {
let command = *command;
let base = CommandBase::new(clap_args, repo_root, version)?;
daemon::main(&command, &base).await?;
Ok(Payload::Rust(Ok(0)))
},
Command::Prune { .. }
| Command::Run(_)
// the daemon itself still delegates to Go
| Command::Daemon { .. } => Ok(Payload::Go(Box::new(clap_args))),
Command::Completion { shell } => {
generate(*shell, &mut Args::command(), "turbo", &mut io::stdout());
Ok(Payload::Rust(Ok(0)))
}
}
}
impl Args {
pub fn ui(&self) -> UI {
if self.no_color {
UI::new(true)
} else if self.color {
UI::new(false)
} else {
UI::infer()
}
}
}
#[cfg(test)]
mod test {
use std::path::PathBuf;
use clap::Parser;
use itertools::Itertools;
use pretty_assertions::assert_eq;
struct CommandTestCase {
command: &'static str,
command_args: Vec<Vec<&'static str>>,
global_args: Vec<Vec<&'static str>>,
expected_output: Args,
}
fn get_default_run_args() -> RunArgs {
RunArgs {
cache_workers: 10,
output_logs: None,
..RunArgs::default()
}
}
impl CommandTestCase {
fn test(&self) {
let permutations = self.create_all_arg_permutations();
for command in permutations {
assert_eq!(Args::try_parse_from(command).unwrap(), self.expected_output)
}
}
fn create_all_arg_permutations(&self) -> Vec<Vec<&'static str>> {
let mut permutations = Vec::new();
let mut global_args = vec![vec![self.command]];
global_args.extend(self.global_args.clone());
let global_args_len = global_args.len();
let command_args_len = self.command_args.len();
// Iterate through all the different permutations of args
for global_args_permutation in global_args.into_iter().permutations(global_args_len) {
let command_args = self.command_args.clone();
for command_args_permutation in
command_args.into_iter().permutations(command_args_len)
{
let mut command = vec![vec!["turbo"]];
command.extend(global_args_permutation.clone());
command.extend(command_args_permutation);
permutations.push(command.into_iter().flatten().collect())
}
}
permutations
}
}
use anyhow::Result;
use crate::cli::{Args, Command, DryRunMode, EnvMode, OutputLogsMode, RunArgs, Verbosity};
#[test]
fn test_parse_run() -> Result<()> {
assert_eq!(
Args::try_parse_from(["turbo", "run", "build"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
env_mode: EnvMode::Infer,
..get_default_run_args()
}))),
..Args::default()
},
"env_mode: default infer"
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--experimental-env-mode"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
env_mode: EnvMode::Infer,
..get_default_run_args()
}))),
..Args::default()
},
"env_mode: not fully-specified"
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--experimental-env-mode", "infer"])
.unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
env_mode: EnvMode::Infer,
..get_default_run_args()
}))),
..Args::default()
},
"env_mode: specified infer"
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--experimental-env-mode", "loose"])
.unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
env_mode: EnvMode::Loose,
..get_default_run_args()
}))),
..Args::default()
},
"env_mode: specified loose"
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--experimental-env-mode", "strict"])
.unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
env_mode: EnvMode::Strict,
..get_default_run_args()
}))),
..Args::default()
},
"env_mode: specified strict"
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "lint", "test"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string(), "lint".to_string(), "test".to_string()],
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--cache-dir", "foobar"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
cache_dir: Some("foobar".to_string()),
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--cache-workers", "100"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
cache_workers: 100,
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--concurrency", "20"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
concurrency: Some("20".to_string()),
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--continue"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
continue_execution: true,
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--dry-run"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
dry_run: Some(DryRunMode::Text),
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--dry-run", "json"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
dry_run: Some(DryRunMode::Json),
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from([
"turbo", "run", "build", "--filter", "water", "--filter", "earth", "--filter",
"fire", "--filter", "air"
])
.unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
filter: vec![
"water".to_string(),
"earth".to_string(),
"fire".to_string(),
"air".to_string()
],
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from([
"turbo", "run", "build", "-F", "water", "-F", "earth", "-F", "fire", "-F", "air"
])
.unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
filter: vec![
"water".to_string(),
"earth".to_string(),
"fire".to_string(),
"air".to_string()
],
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from([
"turbo", "run", "build", "--filter", "water", "-F", "earth", "--filter", "fire",
"-F", "air"
])
.unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
filter: vec![
"water".to_string(),
"earth".to_string(),
"fire".to_string(),
"air".to_string()
],
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--force"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
force: true,
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--global-deps", ".env"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
global_deps: vec![".env".to_string()],
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from([
"turbo",
"run",
"build",
"--global-deps",
".env",
"--global-deps",
".env.development"
])
.unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
global_deps: vec![".env".to_string(), ".env.development".to_string()],
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--graph"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
graph: Some("".to_string()),
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--graph", "out.html"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
graph: Some("out.html".to_string()),
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--ignore", "foo.js"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
ignore: vec!["foo.js".to_string()],
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from([
"turbo", "run", "build", "--ignore", "foo.js", "--ignore", "bar.js"
])
.unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
ignore: vec!["foo.js".to_string(), "bar.js".to_string()],
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--include-dependencies"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
include_dependencies: true,
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--no-cache"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
no_cache: true,
..get_default_run_args()
}))),
..Args::default()
}
);
assert_eq!(
Args::try_parse_from(["turbo", "run", "build", "--no-daemon"]).unwrap(),
Args {
command: Some(Command::Run(Box::new(RunArgs {
tasks: vec!["build".to_string()],
no_daemon: true,
..get_default_run_args()
}))),
..Args::default()
}
);