Skip to content

Commit

Permalink
fix: dont accidentally wipe pyproject.toml on init (#1775)
Browse files Browse the repository at this point in the history
Fixes #1770
  • Loading branch information
ruben-arts authored Aug 9, 2024
1 parent a025c13 commit ffdfb96
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
}

// Create a 'pyproject.toml' manifest
}
if pyproject {
} else if pyproject {
let rv = env
.render_named_str(
consts::PYPROJECT_MANIFEST,
Expand Down
6 changes: 6 additions & 0 deletions tests/common/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ impl InitBuilder {
self.args.channels = Some(vec![]);
self
}

/// Instruct init which manifest format to use
pub fn with_format(mut self, format: init::ManifestFormat) -> Self {
self.args.format = Some(format);
self
}
}

impl IntoFuture for InitBuilder {
Expand Down
20 changes: 19 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,25 @@ impl PixiControl {
}

pub fn manifest_path(&self) -> PathBuf {
self.project_path().join(consts::PROJECT_MANIFEST)
// Either pixi.toml or pyproject.toml
if self.project_path().join(consts::PROJECT_MANIFEST).exists() {
return self.project_path().join(consts::PROJECT_MANIFEST);
} else if self
.project_path()
.join(consts::PYPROJECT_MANIFEST)
.exists()
{
return self.project_path().join(consts::PYPROJECT_MANIFEST);
} else {
return self.project_path().join(consts::PROJECT_MANIFEST);
}
}

/// Get the manifest contents
pub fn manifest_contents(&self) -> miette::Result<String> {
std::fs::read_to_string(self.manifest_path())
.into_diagnostic()
.context("failed to read manifest")
}

/// Initialize pixi project inside a temporary directory. Returns a
Expand Down
34 changes: 33 additions & 1 deletion tests/init_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ mod common;

use std::str::FromStr;

use pixi::cli;
use pixi_manifest::FeaturesExt;
use rattler_conda_types::{NamedChannelOrUrl, Version};
use rattler_conda_types::{NamedChannelOrUrl, Platform, Version};

use crate::common::PixiControl;

Expand Down Expand Up @@ -76,6 +77,37 @@ async fn default_channel() {
)
}

// Test the initialization from an existing pyproject.toml file without the pixi information
#[tokio::test]
async fn init_from_existing_pyproject_toml() {
let pixi = PixiControl::new().unwrap();

// Copy the pyproject.toml file to the project directory
let project_path = pixi.project_path();
let pyproject_toml = project_path.join("pyproject.toml");
let pyproject_toml_contents = include_str!("pixi_tomls/pyproject_no_pixi.toml");
std::fs::write(&pyproject_toml, pyproject_toml_contents).unwrap();

// Init a new project
pixi.init()
.with_format(cli::init::ManifestFormat::Pyproject)
.await
.unwrap();

// Check if the new manifest still contains all the same data as before
assert!(pixi
.manifest_contents()
.unwrap()
.contains(pyproject_toml_contents));

// Check if the new manifest is readable by pixi and contains the default values
let project = pixi.project().unwrap();
assert!(project
.default_environment()
.platforms()
.contains(&Platform::current()));
}

// TODO: enable and fix this test when we fix the global config loading
// #[tokio::test]
// async fn default_pypi_config() {
Expand Down
41 changes: 41 additions & 0 deletions tests/pixi_tomls/pyproject_no_pixi.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[build-system]
build-backend = "setuptools.build_meta"
requires = ["setuptools>=61", "setuptools_scm[toml]>=7"]

[project]
authors = [{ name = "ORGNAME", email = "[email protected]" }]
classifiers = ["Development Status :: 1 - Planning"]
dependencies = ["numpy"]
description = "TEST"
dynamic = ["version"]
license.file = "LICENSE"
name = "foo"
readme = "README.md"
requires-python = ">=3.10"

[project.optional-dependencies]
dev = ["pytest >=6", "pytest-cov >=3", "pre-commit"]
test = ["pytest >=6", "pytest-cov >=3", "mypy"]

[project.urls]
Homepage = "https://github.com/ORGNAME/foo"

[tool.setuptools_scm]
write_to = "src/foo/_version.py"


[tool.pytest.ini_options]
addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"]
filterwarnings = ["error"]
log_cli_level = "INFO"
minversion = "6.0"
testpaths = ["tests"]
xfail_strict = true


[tool.coverage]
report.exclude_also = ['\.\.\.', 'if typing.TYPE_CHECKING:']
run.source = ["foo"]

[tool.mypy]
files = ["src", "tests"]

0 comments on commit ffdfb96

Please sign in to comment.