Example #1
0
        /// <inheritdoc cref="IBrowserFetcher.DownloadAsync(string)"/>
        public async Task <RevisionInfo> DownloadAsync(string revision = null)
        {
            revision ??= _preferredRevision;

            string url        = _params(_platform, revision).DownloadURL;
            string zipPath    = Path.Combine(_downloadsFolder, $"download-{_platform.ToString()}-{revision}.zip");
            string folderPath = GetFolderPath(revision);

            if (new DirectoryInfo(folderPath).Exists)
            {
                return(GetRevisionInfo(revision));
            }

            var downloadFolder = new DirectoryInfo(_downloadsFolder);

            if (!downloadFolder.Exists)
            {
                downloadFolder.Create();
            }

            if (DownloadProgressChanged != null)
            {
                _webClient.DownloadProgressChanged += DownloadProgressChanged;
            }

            await _webClient.DownloadFileTaskAsync(new Uri(url), zipPath).ConfigureAwait(false);

            if (_platform == Platform.MacOS)
            {
                // ZipFile and many others unzip libraries have issues extracting .app files
                // Until we have a clear solution we'll call the native unzip tool
                // https://github.com/dotnet/corefx/issues/15516
                NativeExtractToDirectory(zipPath, folderPath);
            }
            else
            {
                ZipFile.ExtractToDirectory(zipPath, folderPath);
            }

            new FileInfo(zipPath).Delete();

            var revisionInfo = GetRevisionInfo(revision);

            if (revisionInfo != null && GetCurrentPlatform() == Platform.Linux)
            {
                if (LinuxSysCall.Chmod(revisionInfo.ExecutablePath, LinuxSysCall.ExecutableFilePermissions) != 0)
                {
                    throw new PlaywrightSharpException($"Unable to chmod the BrowserApp ({Marshal.GetLastWin32Error()})");
                }
            }

            return(revisionInfo);
        }
        /// <summary>
        /// Downloads the revision.
        /// </summary>
        /// <returns>Task which resolves to the completed download.</returns>
        /// <param name="revision">Revision.</param>
        public async Task <RevisionInfo> DownloadAsync(int revision)
        {
            var url        = GetDownloadURL(Platform, DownloadHost, revision);
            var zipPath    = Path.Combine(DownloadsFolder, $"download-{Platform.ToString()}-{revision}.zip");
            var folderPath = GetFolderPath(revision);

            if (new DirectoryInfo(folderPath).Exists)
            {
                return(RevisionInfo(revision));
            }

            var downloadFolder = new DirectoryInfo(DownloadsFolder);

            if (!downloadFolder.Exists)
            {
                downloadFolder.Create();
            }

            var webClient = new WebClient();

            if (DownloadProgressChanged != null)
            {
                webClient.DownloadProgressChanged += DownloadProgressChanged;
            }
            await webClient.DownloadFileTaskAsync(new Uri(url), zipPath).ConfigureAwait(false);

            if (Platform == Platform.MacOS)
            {
                //ZipFile and many others unzip libraries have issues extracting .app files
                //Until we have a clear solution we'll call the native unzip tool
                //https://github.com/dotnet/corefx/issues/15516
                NativeExtractToDirectory(zipPath, folderPath);
            }
            else
            {
                ZipFile.ExtractToDirectory(zipPath, folderPath);
            }

            new FileInfo(zipPath).Delete();

            var revisionInfo = RevisionInfo(revision);

            if (revisionInfo != null && GetCurrentPlatform() == Platform.Linux)
            {
                LinuxSysCall.SetPermissions(revisionInfo.ExecutablePath, BrowserPermissionsInLinux);
            }
            return(revisionInfo);
        }
        public async Task ShouldDownloadAndExtractLinuxBinary()
        {
            var browserFetcher = Puppeteer.CreateBrowserFetcher(new BrowserFetcherOptions
            {
                Platform = Platform.Linux,
                Path     = _downloadsFolder,
                Host     = TestConstants.ServerUrl
            });
            var revisionInfo = browserFetcher.RevisionInfo(123456);

            Server.SetRedirect(revisionInfo.Url.Substring(TestConstants.ServerUrl.Length), "/chromium-linux.zip");
            Assert.False(revisionInfo.Local);
            Assert.Equal(Platform.Linux, revisionInfo.Platform);
            Assert.False(await browserFetcher.CanDownloadAsync(100000));
            Assert.True(await browserFetcher.CanDownloadAsync(123456));

            try
            {
                revisionInfo = await browserFetcher.DownloadAsync(123456);

                Assert.True(revisionInfo.Local);
                Assert.Equal("LINUX BINARY\n", File.ReadAllText(revisionInfo.ExecutablePath));

                if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    Assert.Equal(
                        BrowserFetcher.BrowserPermissionsInLinux,
                        LinuxSysCall.GetFileMode(revisionInfo.ExecutablePath) & BrowserFetcher.BrowserPermissionsInLinux);
                }
                Assert.Equal(new[] { 123456 }, browserFetcher.LocalRevisions());
                browserFetcher.Remove(123456);
                Assert.Empty(browserFetcher.LocalRevisions());

                //Download should return data from a downloaded version
                //This section is not in the Puppeteer test.
                await browserFetcher.DownloadAsync(123456);

                Server.Reset();
                revisionInfo = await browserFetcher.DownloadAsync(123456);

                Assert.True(revisionInfo.Local);
                Assert.Equal("LINUX BINARY\n", File.ReadAllText(revisionInfo.ExecutablePath));
            }
            finally
            {
                EnsureDownloadsFolderIsDeleted();
            }
        }