Skip to content

Commit 91f3863

Browse files
committed
Code cleanup
1 parent 5ebb6c4 commit 91f3863

File tree

4 files changed

+27
-28
lines changed

4 files changed

+27
-28
lines changed

WebDriverManager/DriverConfigs/Impl/ChromeConfig.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ChromeConfig : IDriverConfig
1616
private const string ExactReleaseVersionPatternUrl = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_<version>";
1717

1818
/// <summary>
19-
/// The minimum version required to download chrome drivers from Chrome for Testing API's
19+
/// The minimum version required to download chrome drivers from Chrome for Testing API's
2020
/// </summary>
2121
private static readonly Version MinChromeForTestingDriverVersion = new Version("115.0.5763.0");
2222

@@ -165,13 +165,13 @@ private string GetUrlFromChromeStorage(Architecture architecture)
165165
/// </summary>
166166
/// <param name="version">The desired version to download</param>
167167
/// <returns>Chrome driver version info (version number, revision number, download URLs)</returns>
168-
private ChromeVersionInfo GetVersionFromChromeForTestingApi(string noRevisionVersion)
168+
private ChromeVersionInfo GetVersionFromChromeForTestingApi(string version)
169169
{
170170
var knownGoodVersions = ChromeForTestingClient.GetKnownGoodVersionsWithDownloads();
171171

172172
// Pull latest patch version
173173
_chromeVersionInfo = knownGoodVersions.Versions.LastOrDefault(
174-
cV => cV.Version.Contains(noRevisionVersion)
174+
cV => cV.Version.Contains(version)
175175
);
176176

177177
return _chromeVersionInfo;
@@ -206,7 +206,7 @@ private string GetUrlFromChromeForTestingApi(Architecture architecture)
206206
var result = _chromeVersionInfo.Downloads.ChromeDriver
207207
.FirstOrDefault(driver => driver.Platform == platform);
208208

209-
return result.Url;
209+
return result?.Url;
210210
}
211211

212212
private string GetRawBrowserVersion()

WebDriverManager/DriverManager.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class DriverManager
1515

1616
private IBinaryService _binaryService;
1717
private readonly IVariableService _variableService;
18-
private string _downloadDirectory = Directory.GetCurrentDirectory();
18+
private readonly string _downloadDirectory = Directory.GetCurrentDirectory();
1919

2020
public DriverManager()
2121
{
@@ -36,7 +36,10 @@ public DriverManager(IBinaryService binaryService, IVariableService variableServ
3636

3737
public DriverManager WithProxy(IWebProxy proxy)
3838
{
39-
_binaryService = new BinaryService {Proxy = proxy};
39+
lock (Object)
40+
{
41+
_binaryService = new BinaryService {Proxy = proxy};
42+
}
4043
ChromeForTestingClient.Proxy = proxy;
4144
WebRequest.DefaultWebProxy = proxy;
4245
return this;

WebDriverManager/Services/Impl/BinaryService.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ public string SetupBinary(string url, string zipPath, string binaryPath)
8989
Exception renameException = null;
9090
try
9191
{
92-
string[] files = Directory.GetFiles(stagingDir);
92+
var files = Directory.GetFiles(stagingDir);
9393

9494
// Copy the files and overwrite destination files if they already exist.
95-
foreach (string file in files)
95+
foreach (var file in files)
9696
{
9797
// Use static Path methods to extract only the file name from the path.
9898
var fileName = Path.GetFileName(file);
@@ -116,6 +116,7 @@ public string SetupBinary(string url, string zipPath, string binaryPath)
116116
{
117117
Console.Error.WriteLine(ex.ToString());
118118
}
119+
119120
try
120121
{
121122
RemoveZip(zipPath);
@@ -141,23 +142,19 @@ public string DownloadZip(string url, string destination)
141142
if (File.Exists(destination)) return destination;
142143
if (Proxy == null) CheckProxySystemVariables();
143144

144-
if (Proxy != null)
145-
{
146-
using (var webClient = new WebClient() {Proxy = Proxy})
147-
{
148-
webClient.DownloadFile(new Uri(url), destination);
149-
}
150-
}
151-
else
145+
using (var webClient = new WebClient())
152146
{
153-
using (var webClient = new WebClient())
147+
if (Proxy != null)
154148
{
155-
webClient.DownloadFile(new Uri(url), destination);
149+
webClient.Proxy = Proxy;
156150
}
151+
152+
webClient.DownloadFile(new Uri(url), destination);
157153
}
158154

159155
return destination;
160156
}
157+
161158
protected void CheckProxySystemVariables()
162159
{
163160
const string nameHttp = "HTTP_PROXY";
@@ -187,17 +184,16 @@ protected string UnZip(string path, string destination, string name)
187184
{
188185
foreach (ZipEntry zipEntry in zip)
189186
{
190-
if (zipEntry.Name.EndsWith(name) && zipEntry.IsFile)
187+
if (!zipEntry.Name.EndsWith(name) || !zipEntry.IsFile) continue;
188+
189+
var buffer = new byte[4096];
190+
using (var zipStream = zip.GetInputStream(zipEntry))
191191
{
192-
byte[] buffer = new byte[4096];
193-
using (Stream zipStream = zip.GetInputStream(zipEntry))
192+
// Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
193+
// of the file, but does not waste memory.
194+
using (var streamWriter = File.Create(destination))
194195
{
195-
// Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
196-
// of the file, but does not waste memory.
197-
using (FileStream streamWriter = File.Create(destination))
198-
{
199-
StreamUtils.Copy(zipStream, streamWriter, buffer);
200-
}
196+
StreamUtils.Copy(zipStream, streamWriter, buffer);
201197
}
202198
}
203199
}

WebDriverManager/WebDriverManager.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="AngleSharp" Version="1.1.0" />
21+
<PackageReference Include="AngleSharp" Version="1.1.2" />
2222
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
2323
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
2424
<PackageReference Include="SharpZipLib" Version="1.4.2" />

0 commit comments

Comments
 (0)