From 6ba1023135f812bd88dd9bffecebe1f87878ea66 Mon Sep 17 00:00:00 2001 From: Florian Goessler Date: Fri, 10 Jan 2025 10:36:44 +0100 Subject: [PATCH] fix(datamodel-renderer): sort generator fields deterministically --- .../src/configuration/generator.rs | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/schema-engine/datamodel-renderer/src/configuration/generator.rs b/schema-engine/datamodel-renderer/src/configuration/generator.rs index df95ad06ee5..9b4b4e2b115 100644 --- a/schema-engine/datamodel-renderer/src/configuration/generator.rs +++ b/schema-engine/datamodel-renderer/src/configuration/generator.rs @@ -1,4 +1,5 @@ use crate::value::{Array, Documentation, Env, Text, Value}; +use itertools::Itertools; use psl::PreviewFeature; use std::{borrow::Cow, fmt}; @@ -143,7 +144,7 @@ impl fmt::Display for Generator<'_> { writeln!(f, "binaryTargets = {}", self.binary_targets)?; } - for (k, v) in self.config.iter() { + for (k, v) in self.config.iter().sorted_by_key(|(k, _)| k) { writeln!(f, "{k} = {v}")?; } @@ -198,14 +199,39 @@ mod tests { output = "/dev/null" previewFeatures = ["multiSchema", "postgresqlExtensions"] binaryTargets = [env("BINARY TARGET")] + afterGenerate = ["lambda", [], ["print", ["quote", "done!"]]] + customFeatures = ["enums", "models"] customValue = "meow" otherValue = "purr" - customFeatures = ["enums", "models"] - afterGenerate = ["lambda", [], ["print", ["quote", "done!"]]] } "#]]; let rendered = psl::reformat(&format!("{generator}"), 2).unwrap(); expected.assert_eq(&rendered) } + + #[test] + fn creates_consistent_ordering() { + let mut generator1 = Generator::new("client", Env::value("prisma-client-js")); + generator1.push_config_value("first", "A"); + generator1.push_config_value("second", "B"); + let rendered1 = psl::reformat(&format!("{generator1}"), 2).unwrap(); + + let mut generator2 = Generator::new("client", Env::value("prisma-client-js")); + generator2.push_config_value("second", "B"); + generator2.push_config_value("first", "A"); + let rendered2 = psl::reformat(&format!("{generator2}"), 2).unwrap(); + + let expected = expect![[r#" + generator client { + provider = "prisma-client-js" + previewFeatures = ["multiSchema", "postgresqlExtensions"] + first = "A" + second = "B" + } + "#]]; + + expected.assert_eq(&rendered1); + expected.assert_eq(&rendered2) + } }