diff --git a/tests/testsuite/standard_lib.rs b/tests/testsuite/standard_lib.rs index 1c8a9b2e4f0..23bceaa02d7 100644 --- a/tests/testsuite/standard_lib.rs +++ b/tests/testsuite/standard_lib.rs @@ -17,7 +17,7 @@ struct Setup { real_sysroot: String, } -fn setup() -> Setup { +pub(crate) fn publish_mock_std_registry_packages() { // Our mock sysroot requires a few packages from crates.io, so make sure // they're "published" to crates.io. Also edit their code a bit to make sure // that they have access to our custom crates with custom apis. @@ -83,6 +83,10 @@ fn setup() -> Setup { .add_dep(Dependency::new("rustc-std-workspace-std", "*").optional(true)) .feature("mockbuild", &["rustc-std-workspace-std"]) .publish(); +} + +fn setup() -> Setup { + publish_mock_std_registry_packages(); let p = ProjectBuilder::new(paths::root().join("rustc-wrapper")) .file( @@ -384,6 +388,342 @@ fn check_core() { .run(); } +#[cargo_test(build_std_mock)] +fn build_std_does_not_change_lockfile() { + let setup = setup(); + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + edition = "2021" + "#, + ) + .file( + "src/main.rs", + r#" + fn main() { + std::custom_api(); + } + "#, + ) + .build(); + + p.cargo("generate-lockfile").build_std(&setup).run(); + let gen_lockfile = p.read_lockfile(); + + p.cargo("build").build_std(&setup).run(); + let build_lockfile = p.read_lockfile(); + + assert_eq!(gen_lockfile, build_lockfile); + assert_eq!( + gen_lockfile, + r##"# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "foo" +version = "0.1.0" +"## + ); +} + +#[cargo_test(build_std_mock)] +fn builtins_do_not_show_in_package_diff_status_messages() { + let setup = setup(); + let p = project() + .file("src/lib.rs", "#![no_std]") + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + edition = "2021" + + [dependencies] + registry-dep-using-core = "1.0" + "#, + ) + .build(); + + // New lockfile + p.cargo("generate-lockfile") + .build_std_arg(&setup, "std") + .with_stderr_data(str![[r#" +[UPDATING] `dummy-registry` index +[LOCKING] 1 package [..] +"#]]) + .run(); + + // Updating lockfile + p.cargo("add registry-dep-using-alloc") + .build_std_arg(&setup, "core") + .with_stderr_data(str![[r#" +[UPDATING] `dummy-registry` index +[ADDING] registry-dep-using-alloc v1.0.0 to dependencies + Features: + - mockbuild + - rustc-std-workspace-alloc + - rustc-std-workspace-core +[LOCKING] 1 package to highest compatible version +[ADDING] registry-dep-using-alloc v1.0.0 + +"#]]) + .run(); +} + +#[cargo_test(build_std_mock)] +fn builtins_do_not_show_in_metadata() { + let setup = setup(); + let p = project() + .file("src/lib.rs", "#![no_std]") + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [dependencies] + registry-dep-using-core = "1.0" + "#, + ) + .build(); + + p.cargo("metadata") + .build_std_arg(&setup, "core") + .with_stdout_data( + str![[r#" +{ + "packages": [ + { + "name": "foo", + "version": "0.1.0", + "id": "path+[ROOTURL]/foo#0.1.0", + "license": null, + "license_file": null, + "description": null, + "source": null, + "dependencies": [ + { + "name": "registry-dep-using-core", + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "^1.0", + "kind": null, + "rename": null, + "optional": false, + "uses_default_features": true, + "features": [], + "target": null, + "registry": null + } + ], + "targets": [ + { + "kind": [ + "lib" + ], + "crate_types": [ + "lib" + ], + "name": "foo", + "src_path": "[ROOT]/foo/src/lib.rs", + "edition": "2015", + "doc": true, + "doctest": true, + "test": true + } + ], + "features": {}, + "manifest_path": "[ROOT]/foo/Cargo.toml", + "metadata": null, + "publish": null, + "authors": [], + "categories": [], + "keywords": [], + "readme": null, + "repository": null, + "homepage": null, + "documentation": null, + "edition": "2015", + "links": null, + "default_run": null, + "rust_version": null + }, + { + "name": "registry-dep-using-core", + "version": "1.0.0", + "id": "registry+https://github.com/rust-lang/crates.io-index#registry-dep-using-core@1.0.0", + "license": null, + "license_file": null, + "description": null, + "source": "registry+https://github.com/rust-lang/crates.io-index", + "dependencies": [ + { + "name": "rustc-std-workspace-core", + "source": "registry+https://github.com/rust-lang/crates.io-index", + "req": "*", + "kind": null, + "rename": null, + "optional": true, + "uses_default_features": true, + "features": [], + "target": null, + "registry": null + } + ], + "targets": [ + { + "kind": [ + "lib" + ], + "crate_types": [ + "lib" + ], + "name": "registry_dep_using_core", + "src_path": "[ROOT]/home/.cargo/registry/src/-[HASH]/registry-dep-using-core-1.0.0/src/lib.rs", + "edition": "2015", + "doc": true, + "doctest": true, + "test": true + } + ], + "features": { + "mockbuild": [ + "rustc-std-workspace-core" + ], + "rustc-std-workspace-core": [ + "dep:rustc-std-workspace-core" + ] + }, + "manifest_path": "[ROOT]/home/.cargo/registry/src/-[HASH]/registry-dep-using-core-1.0.0/Cargo.toml", + "metadata": null, + "publish": null, + "authors": [], + "categories": [], + "keywords": [], + "readme": null, + "repository": null, + "homepage": null, + "documentation": null, + "edition": "2015", + "links": null, + "default_run": null, + "rust_version": null + } + ], + "workspace_members": [ + "path+[ROOTURL]/foo#0.1.0" + ], + "workspace_default_members": [ + "path+[ROOTURL]/foo#0.1.0" + ], + "resolve": { + "nodes": [ + { + "id": "path+[ROOTURL]/foo#0.1.0", + "dependencies": [ + "registry+https://github.com/rust-lang/crates.io-index#registry-dep-using-core@1.0.0" + ], + "deps": [ + { + "name": "registry_dep_using_core", + "pkg": "registry+https://github.com/rust-lang/crates.io-index#registry-dep-using-core@1.0.0", + "dep_kinds": [ + { + "kind": null, + "target": null + } + ] + } + ], + "features": [] + }, + { + "id": "registry+https://github.com/rust-lang/crates.io-index#registry-dep-using-core@1.0.0", + "dependencies": [ + ], + "deps": [], + "features": [] + } + ], + "root": "path+[ROOTURL]/foo#0.1.0" + }, + "target_directory": "[ROOT]/foo/target", + "build_directory": "[ROOT]/foo/target", + "version": 1, + "workspace_root": "[ROOT]/foo", + "metadata": null +} +"#]].is_json(), + ) + .run(); +} + +#[cargo_test(build_std_mock)] +fn builtins_do_not_show_in_tree() { + let setup = setup(); + let p = project() + .file("src/lib.rs", "#![no_std]") + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [dependencies] + registry-dep-using-core = "1.0" + "#, + ) + .build(); + + p.cargo("tree") + .build_std_arg(&setup, "core") + .with_stdout_data(str![[r#" +foo v0.1.0 ([ROOT]/foo) +└── registry-dep-using-core v1.0.0 + +"#]]) + .run(); +} + +#[cargo_test(build_std_mock)] +fn builtins_are_not_vendored() { + let setup = setup(); + let p = project() + .file("src/lib.rs", "#![no_std]") + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [dependencies] + registry-dep-using-core = "1.0" + "#, + ) + .build(); + + // --respect-source-config is required to use the crates.io replacement in the test harness + p.cargo("vendor --respect-source-config") + .build_std_arg(&setup, "core") + .run(); + + assert!( + p.root() + .join("vendor/registry-dep-using-core/Cargo.toml") + .is_file() + ); + assert!(!p.root().join("vendor/core").exists()); + assert!(!p.root().join("vendor/compiler_builtins").exists()); +} + #[cargo_test(build_std_mock)] fn build_std_with_no_arg_for_core_only_target() { let target = "aarch64-unknown-none"; diff --git a/tests/testsuite/unit_graph.rs b/tests/testsuite/unit_graph.rs index 806806749fb..dc5fda938f3 100644 --- a/tests/testsuite/unit_graph.rs +++ b/tests/testsuite/unit_graph.rs @@ -1,5 +1,7 @@ //! Tests for --unit-graph option. +use std::path::Path; + use crate::prelude::*; use cargo_test_support::project; use cargo_test_support::registry::Package; @@ -238,3 +240,243 @@ fn simple() { ) .run(); } + +#[cargo_test] +fn builtins() { + crate::standard_lib::publish_mock_std_registry_packages(); + let p = project().file("src/lib.rs", "#![no_std]").build(); + let mock_std = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/testsuite/mock-std/library"); + + p.cargo("build --unit-graph -Zunstable-options -Zbuild-std=core") + .env("__CARGO_TESTS_ONLY_SRC_ROOT", mock_std) + .masquerade_as_nightly_cargo(&["unit-graph", "build-std"]) + .with_stdout_data( + str![[r#" +{ + "roots": [ + 4 + ], + "units": [ + { + "dependencies": [ + { + "extern_crate_name": "build_script_build", + "index": 2, + "noprelude": false, + "nounused": false, + "public": false + } + ], + "features": [], + "is_std": true, + "mode": "build", + "pkg_id": "path+file://[..]/tests/testsuite/mock-std/library/compiler_builtins#0.1.0", + "platform": null, + "profile": { + "codegen_backend": null, + "codegen_units": null, + "debug_assertions": true, + "debuginfo": 2, + "incremental": false, + "lto": "false", + "name": "dev", + "opt_level": "0", + "overflow_checks": true, + "panic": "unwind", + "rpath": false, + "split_debuginfo": "{...}", + "strip": "{...}" + }, + "target": { + "crate_types": [ + "lib" + ], + "doc": true, + "doctest": true, + "edition": "2018", + "kind": [ + "lib" + ], + "name": "compiler_builtins", + "src_path": "[..]/tests/testsuite/mock-std/library/compiler_builtins/src/lib.rs", + "test": true + } + }, + { + "dependencies": [], + "features": [], + "is_std": true, + "mode": "build", + "pkg_id": "path+file://[..]/tests/testsuite/mock-std/library/compiler_builtins#0.1.0", + "platform": null, + "profile": { + "codegen_backend": null, + "codegen_units": null, + "debug_assertions": true, + "debuginfo": 0, + "incremental": false, + "lto": "false", + "name": "dev", + "opt_level": "0", + "overflow_checks": true, + "panic": "unwind", + "rpath": false, + "split_debuginfo": "{...}", + "strip": "{...}" + }, + "target": { + "crate_types": [ + "bin" + ], + "doc": false, + "doctest": false, + "edition": "2018", + "kind": [ + "custom-build" + ], + "name": "build-script-build", + "src_path": "[..]/tests/testsuite/mock-std/library/compiler_builtins/build.rs", + "test": false + } + }, + { + "dependencies": [ + { + "extern_crate_name": "build_script_build", + "index": 1, + "noprelude": false, + "nounused": false, + "public": false + } + ], + "features": [], + "is_std": true, + "mode": "run-custom-build", + "pkg_id": "path+file://[..]/tests/testsuite/mock-std/library/compiler_builtins#0.1.0", + "platform": null, + "profile": { + "codegen_backend": null, + "codegen_units": null, + "debug_assertions": true, + "debuginfo": 2, + "incremental": false, + "lto": "false", + "name": "dev", + "opt_level": "0", + "overflow_checks": false, + "panic": "unwind", + "rpath": false, + "split_debuginfo": "{...}", + "strip": "{...}" + }, + "target": { + "crate_types": [ + "bin" + ], + "doc": false, + "doctest": false, + "edition": "2018", + "kind": [ + "custom-build" + ], + "name": "build-script-build", + "src_path": "[..]/tests/testsuite/mock-std/library/compiler_builtins/build.rs", + "test": false + } + }, + { + "dependencies": [], + "features": [], + "is_std": true, + "mode": "build", + "pkg_id": "path+file://[..]/tests/testsuite/mock-std/library/core#0.1.0", + "platform": null, + "profile": { + "codegen_backend": null, + "codegen_units": null, + "debug_assertions": true, + "debuginfo": 2, + "incremental": false, + "lto": "false", + "name": "dev", + "opt_level": "0", + "overflow_checks": true, + "panic": "unwind", + "rpath": false, + "split_debuginfo": "{...}", + "strip": "{...}" + }, + "target": { + "crate_types": [ + "lib" + ], + "doc": true, + "doctest": true, + "edition": "2018", + "kind": [ + "lib" + ], + "name": "core", + "src_path": "[..]/tests/testsuite/mock-std/library/core/src/lib.rs", + "test": true + } + }, + { + "dependencies": [ + { + "extern_crate_name": "compiler_builtins", + "index": 0, + "noprelude": true, + "nounused": true, + "public": true + }, + { + "extern_crate_name": "core", + "index": 3, + "noprelude": true, + "nounused": true, + "public": true + } + ], + "features": [], + "mode": "build", + "pkg_id": "path+[ROOTURL]/foo#0.0.1", + "platform": null, + "profile": { + "codegen_backend": null, + "codegen_units": null, + "debug_assertions": true, + "debuginfo": 2, + "incremental": false, + "lto": "false", + "name": "dev", + "opt_level": "0", + "overflow_checks": true, + "panic": "unwind", + "rpath": false, + "split_debuginfo": "{...}", + "strip": "{...}" + }, + "target": { + "crate_types": [ + "lib" + ], + "doc": true, + "doctest": true, + "edition": "2015", + "kind": [ + "lib" + ], + "name": "foo", + "src_path": "[ROOT]/foo/src/lib.rs", + "test": true + } + } + ], + "version": 1 +} +"#]] + .is_json(), + ) + .run(); +}