@@ -461,6 +461,89 @@ public void UpdateVersionOverrideInPropsFileWhenItExists_Success()
461461 Assert . DoesNotContain ( @$ "<PackageVersion Include=""X"" VersionOverride=""3.0.0"" />", updatedPropsFile ) ;
462462 }
463463
464+ [ PlatformFact ( Platform . Windows ) ]
465+ public void AddPackageReference_WithCPMEnabled_AddsPackageVersionToProps ( )
466+ {
467+ // Arrange
468+ using var testDirectory = TestDirectory . Create ( ) ;
469+
470+ var propsFile =
471+ @"<Project>
472+ <PropertyGroup>
473+ <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
474+ </PropertyGroup>
475+ </Project>" ;
476+ File . WriteAllText ( Path . Combine ( testDirectory , "Directory.Packages.props" ) , propsFile ) ;
477+
478+ string projectContent =
479+ @"<Project Sdk=""Microsoft.NET.Sdk"">
480+ <PropertyGroup>
481+ <TargetFramework>net6.0</TargetFramework>
482+ </PropertyGroup>
483+ </Project>" ;
484+ var projectPath = Path . Combine ( testDirectory , "projectA.csproj" ) ;
485+ File . WriteAllText ( projectPath , projectContent ) ;
486+
487+ var libraryDependency = new LibraryDependency
488+ {
489+ LibraryRange = new LibraryRange (
490+ name : "X" ,
491+ versionRange : VersionRange . Parse ( "1.0.0" ) ,
492+ typeConstraint : LibraryDependencyTarget . Package )
493+ } ;
494+
495+ var msObject = new MSBuildAPIUtility ( logger : new TestLogger ( ) ) ;
496+
497+ // Act
498+ msObject . AddPackageReference ( projectPath , libraryDependency , noVersion : false ) ;
499+
500+ // Assert
501+ string updatedProjectFile = File . ReadAllText ( projectPath ) ;
502+ string updatedPropsFile = File . ReadAllText ( Path . Combine ( testDirectory , "Directory.Packages.props" ) ) ;
503+
504+ // .csproj should contain versionless PackageReference
505+ Assert . Contains ( @"<PackageReference Include=""X""" , updatedProjectFile ) ;
506+ Assert . DoesNotContain ( "Version=" , updatedProjectFile ) ;
507+
508+ // Directory.Packages.props should contain PackageVersion with version
509+ Assert . Contains ( @"<PackageVersion Include=""X"" Version=""1.0.0""" , updatedPropsFile ) ;
510+ }
511+
512+ [ PlatformFact ( Platform . Windows ) ]
513+ public void AddPackageReference_WithoutCPM_AddsVersionedPackageReference ( )
514+ {
515+ // Arrange
516+ using var testDirectory = TestDirectory . Create ( ) ;
517+
518+ string projectContent =
519+ @"<Project Sdk=""Microsoft.NET.Sdk"">
520+ <PropertyGroup>
521+ <TargetFramework>net6.0</TargetFramework>
522+ </PropertyGroup>
523+ </Project>" ;
524+ var projectPath = Path . Combine ( testDirectory , "projectA.csproj" ) ;
525+ File . WriteAllText ( projectPath , projectContent ) ;
526+
527+ var libraryDependency = new LibraryDependency
528+ {
529+ LibraryRange = new LibraryRange (
530+ name : "X" ,
531+ versionRange : VersionRange . Parse ( "1.0.0" ) ,
532+ typeConstraint : LibraryDependencyTarget . Package )
533+ } ;
534+
535+ var msObject = new MSBuildAPIUtility ( logger : new TestLogger ( ) ) ;
536+
537+ // Act
538+ msObject . AddPackageReference ( projectPath , libraryDependency , noVersion : false ) ;
539+
540+ // Assert
541+ string updatedProjectFile = File . ReadAllText ( projectPath ) ;
542+
543+ // .csproj should contain PackageReference with version
544+ Assert . Contains ( @"<PackageReference Include=""X"" Version=""1.0.0""" , updatedProjectFile ) ;
545+ }
546+
464547 [ Fact ]
465548 public void GetListOfProjectsFromPathArgument_WithProjectFile_ReturnsCorrectPaths ( )
466549 {
0 commit comments