public async Task AddFileFromTemplateAsync_Writes_If_Template_Processing_Is_Successful()
        {
            var mockFilesLocator = new Mock<IFilesLocator>();
            var mockTemplating = new Mock<ITemplating>();
            var mockFileSystem = new MockFileSystem();

            var templateName = "TemplateName";
            var templatePath = "C:\template.cshtml";
            var templateContent = "TemplateContent";
            var outputPath = @"C:\Output.txt";
            var generatedText = "GeneratedText";

            mockFilesLocator.Setup(fl => fl.GetFilePath(templateName, It.IsAny<IEnumerable<string>>()))
                .Returns(templatePath);
            mockFileSystem.WriteAllText(templatePath, templateContent);
            mockTemplating.Setup(templating => templating.RunTemplateAsync(templateContent, null))
                .Returns(Task.FromResult(new TemplateResult()
                {
                    ProcessingException = null,
                    GeneratedText = generatedText
                }));

            var codeGeneratorActionService = new CodeGeneratorActionsService(
                mockTemplating.Object, mockFilesLocator.Object, mockFileSystem);

            await codeGeneratorActionService.AddFileFromTemplateAsync(outputPath,
                    templateName,
                    new[] { "TemplateFolder1", "TemplateFolder2" },
                    null);

            Assert.True(mockFileSystem.FileExists(outputPath));
            Assert.Equal(generatedText, mockFileSystem.ReadAllText(outputPath));
        }
        public async Task AddFileFromTemplateAsync_Throws_If_Template_Processing_Has_Exceptions()
        {
            var mockFilesLocator = new Mock<IFilesLocator>();
            var mockTemplating = new Mock<ITemplating>();
            var mockFileSystem = new MockFileSystem();

            var templateName = "TemplateName";
            var templatePath = "C:\template.cshtml";
            var templateContent = "TemplateContent";
            var processingException = new TemplateProcessingException(new[] { "Error1" }, string.Empty);

            mockFilesLocator.Setup(fl => fl.GetFilePath(templateName, It.IsAny<IEnumerable<string>>()))
                .Returns(templatePath);
            mockFileSystem.WriteAllText(templatePath, templateContent);
            mockTemplating.Setup(templating => templating.RunTemplateAsync(templateContent, null))
                .Returns(Task.FromResult(new TemplateResult()
                {
                    ProcessingException = processingException
                }));

            var codeGeneratorActionService = new CodeGeneratorActionsService(
                mockTemplating.Object, mockFilesLocator.Object, mockFileSystem);

            var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
                await codeGeneratorActionService.AddFileFromTemplateAsync("Dummy",
                    templateName,
                    new[] { "TemplateFolder1", "TemplateFolder2" },
                    null));
            Assert.Equal("There was an error running the template " + templatePath + ": Template Processing Failed:Error1", ex.Message);
        }
        public void FilesLocator_Throws_When_No_Matches_In_SearchPaths()
        {
            MockFileSystem fs = new MockFileSystem();
            var folders = new[] { @"C:\One", @"C:\Two" };
            fs.AddFolders(folders);

            fs.WriteAllText(@"C:\One\template1.cshtml", "");
            fs.WriteAllText(@"C:\Two\template2.cshtml", "");
            FilesLocator locator = new FilesLocator(fs);

            var ex = Assert.Throws<InvalidOperationException>(() => locator.GetFilePath("template.cshtml", folders));
            Assert.Equal(@"A file matching the name template.cshtml was not found within any of the folders: C:\One;C:\Two", ex.Message);
        }
        public void FilesLocator_Throws_When_Multiple_Matches_In_OneSearchPath()
        {
            MockFileSystem fs = new MockFileSystem();
            var folders = new[] { @"C:\One", @"C:\One\Sub1", @"C:\One\Sub2" };
            fs.AddFolders(folders);

            fs.WriteAllText(@"C:\One\Sub1\template.cshtml", "");
            fs.WriteAllText(@"C:\One\Sub2\template.cshtml", "");
            FilesLocator locator = new FilesLocator(fs);

            var ex = Assert.Throws<InvalidOperationException>(() => locator.GetFilePath("template.cshtml", new[] { @"C:\One" }));
            Assert.Equal(@"Multiple files with name template.cshtml found within C:\One", ex.Message);
        }
        public void FilesLocator_Returns_Entry_From_FirstSearchPath()
        {
            MockFileSystem fs = new MockFileSystem();
            var folders = new[] { @"C:\One", @"C:\Two" };
            fs.AddFolders(folders);

            fs.WriteAllText(@"C:\One\template.cshtml", "");
            fs.WriteAllText(@"C:\Two\template.cshtml", "");
            FilesLocator locator = new FilesLocator(fs);

            var result = locator.GetFilePath("template.cshtml", folders);

            Assert.Equal(@"C:\One\template.cshtml", result);
        }
        public PackageInstallerTests()
        {
            _mockLogger = new Mock<ILogger>();
            _mockApp = new Mock<IApplicationEnvironment>();
            _mockFileSystem = new MockFileSystem();

            var applicationBasePath = @"C:\App";
            _projectJsonPath = Path.Combine(applicationBasePath, "project.json");

            _mockApp.Setup(app => app.ApplicationBasePath)
                .Returns(applicationBasePath);

            _packageInstaller = new PackageInstaller(_mockLogger.Object, _mockApp.Object, _mockFileSystem);
        }