Exemplo n.º 1
0
    public async Task Publish_by_msbuild_Test()
    {
        // Given
        var solutionDir = FileIO.FindContainerDirToAncestor("*.sln");
        var srcDir      = Path.Combine(solutionDir, "SampleApps", "BlazorWasmApp0");

        using var workDir = WorkDirectory.CreateCopyFrom(srcDir, dst => dst.Name is not "obj" and not "bin");

        for (var i = 0; i < 2; i++)
        {
            Console.WriteLine($"{(i == 0 ? "1st" : "2nd")} time publishing...");

            // When
            await XProcess.Start("dotnet", "restore", workDir).WaitForExitAsync();

            var dotnetCLI = await XProcess.Start("dotnet", "msbuild -p:Configuration=Debug -p:BlazorEnableCompression=false -p:DeployOnBuild=true -p:PublishUrl=bin/publish", workDir).WaitForExitAsync();

            dotnetCLI.ExitCode.Is(0, message: dotnetCLI.StdOutput + dotnetCLI.StdError);

            // Then

            // Validate prerendered contents.
            var wwwrootDir = Path.Combine(workDir, "bin", "publish", "wwwroot");
            ValidatePrerenderedContents_of_BlazorWasmApp0(wwwrootDir);
        }
    }
Exemplo n.º 2
0
    public async Task Output_of_ProjectB_Contains_DriverFile_Test()
    {
        var vsAppDir = Environment.GetEnvironmentVariable("VSAPPIDDIR");

        if (vsAppDir == null)
        {
            Assert.Inconclusive(@"This test requires Visual Studio and the definition of the ""VSAPPDIR"" environment variable to point out the directory where Visual Studio ""devenv.exe"" exists. (ex: VSAPPDIR=C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\)");
        }

        var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj");

        using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "ProjectAB"), item => item.Name is not "obj" and not "bin");

        var nuget  = Path.Combine(unitTestProjectDir, "..", "buildTools", "nuget.exe");
        var devenv = Path.Combine(vsAppDir, "devenv.exe");
        await XProcess.Start(nuget, "restore", workDir).ExitCodeIs(0);

        await XProcess.Start(devenv, "ProjectAB.sln /Build", workDir).ExitCodeIs(0);

        var outDir          = Path.Combine(workDir, "ProjectB", "bin", "Debug", "net472");
        var driverFullPath1 = Path.Combine(outDir, "geckodriver");
        var driverFullPath2 = Path.Combine(outDir, "geckodriver.exe");

        (File.Exists(driverFullPath1) || File.Exists(driverFullPath2)).IsTrue();
    }
Exemplo n.º 3
0
    public static WorkDirectory CreateSampleAppsWorkDir()
    {
        var solutionDir      = FileIO.FindContainerDirToAncestor("*.sln");
        var srcSampleAppsDir = Path.Combine(solutionDir, "SampleApps");

        return(WorkDirectory.CreateCopyFrom(srcSampleAppsDir, arg => arg.Name is (not "obj" and not "bin")));
    }
Exemplo n.º 4
0
    public async Task Publish_with_SingleFileEnabled_Test()
    {
        var rid                  = "win-x64";
        var driverFileName       = "chromedriver.exe";
        var executableFileFormat = Format.PE32;

        var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj");

        using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "Project"), predicate: item => item.Name is not "obj" and not "bin");

        var publishCommand = new[] {
            "dotnet", "publish", "-r", rid, "-o", "out",
            "-c:Release",
            "-p:PublishChromeDriver=true",
            "-p:PublishSingleFile=true",
            "-p:SelfContained=false"
        };

        // IMPORTANT: 2nd time of publishing, sometimes lost driver file in the published folder, so we have to validate it..
        for (var i = 0; i < 2; i++)
        {
            using var dotnet = await XProcess.Start(
                      filename : publishCommand.First(),
                      arguments : String.Join(' ', publishCommand.Skip(1)),
                      workDir).WaitForExitAsync();

            dotnet.ExitCode.Is(0);

            var driverFullPath = Path.Combine(workDir, "out", driverFileName);
            File.Exists(driverFullPath).IsTrue();

            DetectFormat(driverFullPath).Is(executableFileFormat);
        }
    }
Exemplo n.º 5
0
    public async Task Publish_Test()
    {
        // Given
        var solutionDir = FileIO.FindContainerDirToAncestor("*.sln");
        var srcDir      = Path.Combine(solutionDir, "SampleApps", "BlazorWasmApp0");

        using var workDir = WorkDirectory.CreateCopyFrom(srcDir, dst => dst.Name is not "obj" and not "bin");

        var expectedHomeTitles = new Dictionary <int, string> {
            [1] = "Home", [2] = "My Home"
        };
        var expectedEnvNames = new Dictionary <int, string> {
            [1] = "Prerendering", [2] = "Foo"
        };

        for (var i = 1; i <= 2; i++)
        {
            Console.WriteLine($"{(i == 1 ? "1st" : "2nd")} time publishing...");

            // When
            var arg = "publish -c:Debug -p:BlazorEnableCompression=false -o:bin/publish";
            // if 2nd time publishing, override the environment name.
            if (i == 2)
            {
                arg += " -p:BlazorWasmPrerenderingEnvironment=" + expectedEnvNames[2];
            }

            var dotnetCLI = await XProcess.Start("dotnet", arg, workDir).WaitForExitAsync();

            dotnetCLI.ExitCode.Is(0, message: dotnetCLI.StdOutput + dotnetCLI.StdError);

            // Then

            // Validate prerendered contents.
            var wwwrootDir = Path.Combine(workDir, "bin", "publish", "wwwroot");
            ValidatePrerenderedContents_of_BlazorWasmApp0(
                wwwrootDir,
                homeTitle: expectedHomeTitles[i],
                environment: expectedEnvNames[i]);

            // Validate PWA assets manifest.
            var indexHtmlBytes = File.ReadAllBytes(Path.Combine(wwwrootDir, "index.html"));
            using var sha256 = SHA256.Create();
            var hash = "sha256-" + Convert.ToBase64String(sha256.ComputeHash(indexHtmlBytes));

            var serviceWorkerAssetsJs = File.ReadAllText(Path.Combine(wwwrootDir, "my-assets.js"));
            serviceWorkerAssetsJs = Regex.Replace(serviceWorkerAssetsJs, @"^self\.assetsManifest\s*=\s*", "");
            serviceWorkerAssetsJs = Regex.Replace(serviceWorkerAssetsJs, ";\\s*$", "");
            var assetsManifestFile = JsonSerializer.Deserialize <AssetsManifestFile>(serviceWorkerAssetsJs);
            var assetManifestEntry = assetsManifestFile?.assets?.First(a => a.url == "index.html");
            assetManifestEntry.IsNotNull();
            assetManifestEntry !.hash.Is(hash);
        }
    }
Exemplo n.º 6
0
    public async Task Publish_with_HTTP500_Test()
    {
        // Given
        var solutionDir = FileIO.FindContainerDirToAncestor("*.sln");
        var srcDir      = Path.Combine(solutionDir, "SampleApps", "BlazorWasmApp1");

        using var workDir = WorkDirectory.CreateCopyFrom(srcDir, dst => dst.Name is not "obj" and not "bin");

        // When (Set the hoting environment name to "ExceptionTest")
        var dotnetCLI = await XProcess.Start("dotnet", "publish -c:Release -p:BlazorWasmPrerenderingEnvironment=ExceptionTest -p:BlazorEnableCompression=false --nologo", workDir).WaitForExitAsync();

        // Then (Exit code is NOT 0)
        dotnetCLI.ExitCode.IsNot(0, message: dotnetCLI.Output);
    }
Exemplo n.º 7
0
    public async Task PublishWithRuntimeIdentifier_NoPublish_Test(string rid, string driverFileName, Format _)
    {
        var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj");

        using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "Project"), predicate: item => item.Name is not "obj" and not "bin");

        using var dotnet = await XProcess.Start("dotnet", $"publish -r {rid} -o out", workDir).WaitForExitAsync();

        dotnet.ExitCode.Is(0);

        var driverFullPath = Path.Combine(workDir, "out", driverFileName);

        File.Exists(driverFullPath).IsFalse();
    }
Exemplo n.º 8
0
    public async ValueTask <WorkDirectory> PublishAsync()
    {
        if (!this.PublishedOnce)
        {
            var publishProcess = XProcess.Start(
                "dotnet",
                $"publish -c:{this.Configuration} -p:BlazorWasmPrerendering=disable -p:BlazorEnableCompression=false -p:UsingBrowserRuntimeWorkload=false",
                workingDirectory: this.ProjectDir);
            await publishProcess.WaitForExitAsync();

            publishProcess.ExitCode.Is(0, message: publishProcess.StdOutput + publishProcess.StdError);
            this.PublishedOnce = true;
        }

        return(WorkDirectory.CreateCopyFrom(this.PublishSrcDir, _ => true));
    }
Exemplo n.º 9
0
    public async Task PublishWithRuntimeIdentifier_with_DefineConstants_Test(string rid, string driverFileName, Format executableFileFormat)
    {
        var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj");

        using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "Project"), predicate: item => item.Name is not "obj" and not "bin");

        using var dotnet = await XProcess.Start("dotnet", $"publish -r {rid} -o out -p:DefineConstants=_PUBLISH_CHROMEDRIVER", workDir).WaitForExitAsync();

        dotnet.ExitCode.Is(0);

        var driverFullPath = Path.Combine(workDir, "out", driverFileName);

        File.Exists(driverFullPath).IsTrue();

        DetectFormat(driverFullPath).Is(executableFileFormat);
    }
Exemplo n.º 10
0
    public WorkSpace(string startupProjDir, string framework, string configuration)
    {
        this.WorkSpaceDir = WorkDirectory.CreateCopyFrom(GetTestDir(), predicate: item => item.Name is not "obj" and not "bin" and not ".vs");

        var nugetConfigPath = Path.Combine(this.WorkSpaceDir, "nuget.config");

        if (File.Exists(nugetConfigPath))
        {
            File.Delete(nugetConfigPath);
        }

        this.StartupProj = Path.Combine(this.WorkSpaceDir, startupProjDir);
        this.Bin         = Path.Combine(this.StartupProj, "bin");
        this.Obj         = Path.Combine(this.StartupProj, "obj");
        this.OutputDir   = Path.Combine(this.Bin, configuration, framework);
        this.PublishDir  = Path.Combine(this.OutputDir, "publish");
    }
Exemplo n.º 11
0
    public async Task Output_of_ProjectB_Contains_DriverFile_Test()
    {
        var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj");

        using var workDir = WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "ProjectAB"), item => item.Name is not "obj" and not "bin");

        //var devenv = Environment.ExpandEnvironmentVariables(Path.Combine("%DevEnvDir%", "devenv.exe"));
        var devenv = @"C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\devenv.exe";

        using var nugetProcess = await XProcess.Start("nuget", "restore", workDir).WaitForExitAsync();

        nugetProcess.ExitCode.Is(0);
        using var devenvProcess = await XProcess.Start(devenv, "ProjectAB.sln /Build", workDir).WaitForExitAsync();

        devenvProcess.ExitCode.Is(0);

        var outDir          = Path.Combine(workDir, "ProjectB", "bin", "Debug", "net472");
        var driverFullPath1 = Path.Combine(outDir, "chromedriver");
        var driverFullPath2 = Path.Combine(outDir, "chromedriver.exe");

        (File.Exists(driverFullPath1) || File.Exists(driverFullPath2)).IsTrue();
    }
Exemplo n.º 12
0
    public async Task AppSettings_Test()
    {
        // Given
        var solutionDir = FileIO.FindContainerDirToAncestor("*.sln");
        var srcDir      = Path.Combine(solutionDir, "SampleApps", "BlazorWasmApp0");

        using var workDir = WorkDirectory.CreateCopyFrom(srcDir, dst => dst.Name is not "obj" and not "bin");

        File.WriteAllText(Path.Combine(workDir, "wwwroot", "appsettings.json"), @"{""HomeTitle"":""127.0.0.1""}");

        // When
        var dotnetCLI = await XProcess.Start("dotnet", "publish -c:Debug -p:BlazorEnableCompression=false -o:bin/publish", workDir).WaitForExitAsync();

        dotnetCLI.ExitCode.Is(0, message: dotnetCLI.StdOutput + dotnetCLI.StdError);

        // Then

        // Validate prerendered contents.
        var wwwrootDir = Path.Combine(workDir, "bin", "publish", "wwwroot");

        ValidatePrerenderedContents_of_BlazorWasmApp0(wwwrootDir, homeTitle: "127.0.0.1");
    }
Exemplo n.º 13
0
    private WorkDirectory CreateWorkDir()
    {
        var unitTestProjectDir = FileIO.FindContainerDirToAncestor("*.csproj");

        return(WorkDirectory.CreateCopyFrom(Path.Combine(unitTestProjectDir, "Project"), predicate: item => item.Name is not "obj" and not "bin"));
    }