public void Fails_WhenStaticWebAsset_DoesNotContainContentRoot()
        {
            // Arrange
            var errorMessages = new List <string>();
            var buildEngine   = new Mock <IBuildEngine>();

            buildEngine.Setup(e => e.LogErrorEvent(It.IsAny <BuildErrorEventArgs>()))
            .Callback <BuildErrorEventArgs>(args => errorMessages.Add(args.Message));

            var task = new GenerateStaticWebAsssetsPropsFile
            {
                BuildEngine     = buildEngine.Object,
                StaticWebAssets = new TaskItem[]
                {
                    CreateItem(Path.Combine("wwwroot", "js", "sample.js"), new Dictionary <string, string>
                    {
                        ["SourceType"]   = "",
                        ["SourceId"]     = "MyLibrary",
                        ["BasePath"]     = "_content/mylibrary",
                        ["RelativePath"] = Path.Combine("js", "sample.js"),
                    })
                }
            };

            // Act
            var result = task.Execute();

            // Assert
            Assert.False(result);
            var message = Assert.Single(errorMessages);

            Assert.Equal($"Missing required metadata 'ContentRoot' for '{Path.Combine("wwwroot", "js", "sample.js")}'.", message);
        }
        public void WritesPropsFile_WhenThereIsAtLeastOneStaticAsset()
        {
            // Arrange
            var file             = Path.GetTempFileName();
            var expectedDocument = @"<Project>
  <ItemGroup>
    <StaticWebAsset Include=""$(MSBuildThisFileDirectory)..\staticwebassets\**"">
      <SourceType>Package</SourceType>
      <SourceId>MyLibrary</SourceId>
      <ContentRoot>$(MSBuildThisFileDirectory)..\staticwebassets\</ContentRoot>
      <BasePath>_content/mylibrary</BasePath>
      <RelativePath>%(RecursiveDir)%(FileName)%(Extension)</RelativePath>
    </StaticWebAsset>
  </ItemGroup>
</Project>";

            try
            {
                var buildEngine = new Mock <IBuildEngine>();

                var task = new GenerateStaticWebAsssetsPropsFile
                {
                    BuildEngine         = buildEngine.Object,
                    TargetPropsFilePath = file,
                    StaticWebAssets     = new TaskItem[]
                    {
                        CreateItem(Path.Combine("wwwroot", "js", "sample.js"), new Dictionary <string, string>
                        {
                            ["SourceType"]   = "",
                            ["SourceId"]     = "MyLibrary",
                            ["ContentRoot"]  = @"$(MSBuildThisFileDirectory)..\staticwebassets",
                            ["BasePath"]     = "_content/mylibrary",
                            ["RelativePath"] = Path.Combine("js", "sample.js"),
                        }),
                    }
                };

                // Act
                var result = task.Execute();

                // Assert
                Assert.True(result);
                var document = File.ReadAllText(file);
                Assert.Equal(expectedDocument, document, ignoreLineEndingDifferences: true);
            }
            finally
            {
                if (File.Exists(file))
                {
                    File.Delete(file);
                }
            }
        }
        public void Fails_WhenStaticWebAsset_HaveDifferentBasePath()
        {
            // Arrange
            var expectedError = "Static web assets have different 'BasePath' metadata values " +
                                "'_content/mylibrary' and '_content/mylibrary2' " +
                                $"for '{Path.Combine("wwwroot", "js", "sample.js")}' and '{Path.Combine("wwwroot", "css", "site.css")}'.";

            var errorMessages = new List <string>();
            var buildEngine   = new Mock <IBuildEngine>();

            buildEngine.Setup(e => e.LogErrorEvent(It.IsAny <BuildErrorEventArgs>()))
            .Callback <BuildErrorEventArgs>(args => errorMessages.Add(args.Message));

            var task = new GenerateStaticWebAsssetsPropsFile
            {
                BuildEngine     = buildEngine.Object,
                StaticWebAssets = new TaskItem[]
                {
                    CreateItem(Path.Combine("wwwroot", "js", "sample.js"), new Dictionary <string, string>
                    {
                        ["SourceType"]   = "",
                        ["SourceId"]     = "MyLibrary",
                        ["ContentRoot"]  = @"$(MSBuildThisFileDirectory)..\staticwebassets",
                        ["BasePath"]     = "_content/mylibrary",
                        ["RelativePath"] = Path.Combine("js", "sample.js"),
                    }),
                    CreateItem(Path.Combine("wwwroot", "css", "site.css"), new Dictionary <string, string>
                    {
                        ["SourceType"]   = "",
                        ["SourceId"]     = "MyLibrary",
                        ["ContentRoot"]  = @"$(MSBuildThisFileDirectory)..\staticwebassets",
                        ["BasePath"]     = "_content/mylibrary2",
                        ["RelativePath"] = Path.Combine("css", "site.css"),
                    })
                }
            };

            // Act
            var result = task.Execute();

            // Assert
            Assert.False(result);
            var message = Assert.Single(errorMessages);

            Assert.Equal(expectedError, message);
        }
        public void WritesIndividualItems_WithTheirRespectiveBaseAndRelativePaths()
        {
            // Arrange
            var file             = Path.GetTempFileName();
            var expectedDocument = @"<Project>
  <ItemGroup>
    <StaticWebAsset Include=""$(MSBuildThisFileDirectory)..\staticwebassets\App.styles.css"">
      <SourceType>Package</SourceType>
      <SourceId>MyLibrary</SourceId>
      <ContentRoot>$(MSBuildThisFileDirectory)..\staticwebassets\</ContentRoot>
      <BasePath>/</BasePath>
      <RelativePath>App.styles.css</RelativePath>
    </StaticWebAsset>
    <StaticWebAsset Include=""$(MSBuildThisFileDirectory)..\staticwebassets\js\sample.js"">
      <SourceType>Package</SourceType>
      <SourceId>MyLibrary</SourceId>
      <ContentRoot>$(MSBuildThisFileDirectory)..\staticwebassets\</ContentRoot>
      <BasePath>_content/mylibrary</BasePath>
      <RelativePath>js/sample.js</RelativePath>
    </StaticWebAsset>
  </ItemGroup>
</Project>";

            try
            {
                var buildEngine = new Mock <IBuildEngine>();

                var task = new GenerateStaticWebAsssetsPropsFile
                {
                    BuildEngine         = buildEngine.Object,
                    TargetPropsFilePath = file,
                    StaticWebAssets     = new TaskItem[]
                    {
                        CreateItem(Path.Combine("wwwroot", "js", "sample.js"), new Dictionary <string, string>
                        {
                            ["SourceType"]   = "",
                            ["SourceId"]     = "MyLibrary",
                            ["ContentRoot"]  = @"$(MSBuildThisFileDirectory)..\staticwebassets",
                            ["BasePath"]     = "_content/mylibrary",
                            ["RelativePath"] = Path.Combine("js", "sample.js").Replace("\\", "/"),
                        }),
                        CreateItem(Path.Combine("wwwroot", "App.styles.css"), new Dictionary <string, string>
                        {
                            ["SourceType"]   = "",
                            ["SourceId"]     = "MyLibrary",
                            ["ContentRoot"]  = @"$(MSBuildThisFileDirectory)..\staticwebassets",
                            ["BasePath"]     = "/",
                            ["RelativePath"] = "App.styles.css",
                        }),
                    }
                };

                // Act
                var result = task.Execute();

                // Assert
                Assert.True(result);
                var document = File.ReadAllText(file);
                Assert.Equal(expectedDocument, document, ignoreLineEndingDifferences: true);
            }
            finally
            {
                if (File.Exists(file))
                {
                    File.Delete(file);
                }
            }
        }