Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Jul 12, 2024
1 parent e4d278e commit 5dcfd13
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 19 deletions.
1 change: 1 addition & 0 deletions crates/turborepo-lib/src/cli/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum Error {
#[error(transparent)]
ChromeTracing(#[from] crate::tracing::Error),
#[error(transparent)]
#[diagnostic(transparent)]
BuildPackageGraph(#[from] package_graph::builder::Error),
#[error(transparent)]
Rewrite(#[from] RewriteError),
Expand Down
1 change: 1 addition & 0 deletions crates/turborepo-lib/src/run/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub enum Error {
#[diagnostic(transparent)]
Config(#[from] config::Error),
#[error(transparent)]
#[diagnostic(transparent)]
PackageGraphBuilder(#[from] package_graph::builder::Error),
#[error(transparent)]
DaemonConnector(#[from] daemon::DaemonConnectorError),
Expand Down
11 changes: 5 additions & 6 deletions crates/turborepo-repository/src/package_graph/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
collections::{BTreeMap, HashMap, HashSet},
};

use miette::Diagnostic;
use petgraph::graph::{Graph, NodeIndex};
use tracing::{warn, Instrument};
use turbopath::{
Expand Down Expand Up @@ -33,13 +34,11 @@ pub struct PackageGraphBuilder<'a, T> {
package_discovery: T,
}

#[derive(Debug, thiserror::Error)]
#[derive(Debug, Diagnostic, thiserror::Error)]
pub enum Error {
#[error("could not resolve workspaces: {0}")]
PackageManager(
#[from] crate::package_manager::Error,
#[backtrace] Backtrace,
),
#[error("could not resolve workspaces")]
#[diagnostic(transparent)]
PackageManager(#[from] crate::package_manager::Error),
#[error(
"Failed to add workspace \"{name}\" from \"{path}\", it already exists at \
\"{existing_path}\""
Expand Down
11 changes: 6 additions & 5 deletions crates/turborepo-repository/src/package_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use itertools::{Either, Itertools};
use lazy_regex::{lazy_regex, Lazy};
use miette::{Diagnostic, NamedSource, SourceSpan};
use npm::NpmDetector;
use node_semver::SemverError;
use regex::Regex;
use serde::{Deserialize, Serialize};
use thiserror::Error;
Expand Down Expand Up @@ -285,10 +286,10 @@ pub enum Error {
#[error("We detected multiple package managers in your repository: {}. Please remove one \
of them.", managers.join(", "))]
MultiplePackageManagers { managers: Vec<String> },
#[error("invalid semantic version `{version}`")]
#[error("invalid semantic version: {explanation}")]
#[diagnostic(code(invalid_semantic_version))]
InvalidVersion {
version: String,
explanation: String,
#[label("version found here")]
span: Option<SourceSpan>,
#[source_code]
Expand All @@ -302,7 +303,7 @@ pub enum Error {
#[error(transparent)]
Path(#[from] turbopath::PathError),
#[error(
"could not parse the packageManager field in package.json,\nexpected to match regular \
"could not parse the packageManager field in package.json, expected to match regular \
expression {pattern}"
)]
#[diagnostic(code(invalid_package_manager_field))]
Expand Down Expand Up @@ -456,10 +457,10 @@ impl PackageManager {
};

let (manager, version) = Self::parse_package_manager_string(package_manager)?;
let version = version.parse().map_err(|_| {
let version = version.parse().map_err(|err: SemverError| {
let (span, text) = package_manager.span_and_text("package.json");
Error::InvalidVersion {
version: version.to_string(),
explanation: err.to_string(),
span,
text,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"scripts": {
"something": "turbo run build"
},
"packageManager": "bower",
"workspaces": [
"apps/**",
"packages/**"
Expand Down
46 changes: 46 additions & 0 deletions turborepo-tests/integration/tests/invalid-package-json.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,54 @@ Setup
$ . ${TESTDIR}/../../helpers/setup_integration_test.sh
Clear name field
$ jq '.name = ""' apps/my-app/package.json > package.json.new
$ mv apps/my-app/package.json apps/my-app/package.json.old
$ mv package.json.new apps/my-app/package.json
Build should fail due to missing name field
$ ${TURBO} build 1> ERR
[1]
$ grep -F --quiet 'x package.json must have a name field:' ERR

Restore name field
$ mv apps/my-app/package.json.old apps/my-app/package.json

Clear add invalid packageManager field
$ jq '.packageManager = "[email protected]"' package.json > package.json.new
$ mv package.json.new package.json

Build should fail due to invalid packageManager field (sed removes the square brackets)
$ ${TURBO} build 2>&1 | sed 's/\[\([^]]*\)\]/\\1/g'
invalid_package_manager_field

x could not resolve workspaces
`-> could not parse the packageManager field in package.json, expected to
match regular expression (?P<manager>bun|npm|pnpm|yarn)@(?P<version>\d+
\.\d+\.\d+(-.+)?)
,-\1
5 | },
6 | "packageManager": "[email protected]",
: ^^^^^^^|^^^^^^
: `-- invalid `packageManager` field
7 | "workspaces": [
`----


Add invalid packageManager field that passes the regex.
$ jq '.packageManager = "npm@0.3.211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"' package.json > package.json.new
$ mv package.json.new package.json

$ ${TURBO} build 2>&1 | sed 's/\[\([^]]*\)\]/\(\1)/g'
invalid_semantic_version

x could not resolve workspaces
`-> invalid semantic version: Failed to parse an integer component of a
semver string: number too large to fit in target type
,-\(.*/package.json:5:1\) (re)
5 | },
6 | "packageManager": "npm@0.3.211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
: `-- version found here
7 | "workspaces": [
`----



25 changes: 17 additions & 8 deletions turborepo-tests/integration/tests/recursive-turbo.t
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
Setup
$ . ${TESTDIR}/../../helpers/setup_integration_test.sh

We write into a file because prysk doesn't play well
with the square brackets miette uses for source file paths
$ ${TURBO} something > tmp.log 2>&1
[1]
$ grep --quiet -E "root task //#something \(turbo run build\) looks like it invokes turbo and" tmp.log
$ grep --quiet -E "might cause a loop" tmp.log
$ grep --quiet -E "task found here" tmp.log
$ grep --quiet -E "\"something\": \"turbo run build\"" tmp.log
sed replaces the square brackets with parentheses so prysk can parse the file path
$ ${TURBO} something 2>&1 | sed 's/\[\([^]]*\)\]/\(\1)/g'
\xe2\x80\xa2 Packages in scope: //, another, my-app, util (esc)
\xe2\x80\xa2 Running something in 4 packages (esc)
\xe2\x80\xa2 Remote caching disabled (esc)
x root task //#something (turbo run build) looks like it invokes turbo and
| might cause a loop
,-\(.*/package.json:3:1\) (re)
3 | "scripts": {
4 | "something": "turbo run build"
: ^^^^^^^^|^^^^^^^^
: `-- task found here
5 | },
`----





0 comments on commit 5dcfd13

Please sign in to comment.