Skip to content

Commit 44e4ae9

Browse files
authored
Merge pull request #30 from browserstack/LOC_3186_mac_support
Add support for macOS
2 parents 534031c + 6206a26 commit 44e4ae9

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-22
lines changed

BrowserStackLocal/BrowserStackLocal Unit Tests/BrowserStackTunnelTests.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ namespace BrowserStack_Unit_Tests
1313
[TestClass]
1414
public class BrowserStackTunnelTests
1515
{
16+
static readonly OperatingSystem os = Environment.OSVersion;
17+
static readonly string homepath = os.Platform.ToString() == "Unix" ?
18+
Environment.GetFolderPath(Environment.SpecialFolder.Personal) :
19+
Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
20+
static readonly string binaryName = os.Platform.ToString() == "Unix" ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal.exe";
1621
private TunnelClass tunnel;
1722
[TestMethod]
1823
public void TestInitialState()
@@ -33,17 +38,17 @@ public void TestBinaryPathOnNull()
3338
{
3439
tunnel = new TunnelClass();
3540
tunnel.addBinaryPath(null);
36-
string expectedPath = Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack");
37-
expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe");
41+
string expectedPath = Path.Combine(homepath, ".browserstack");
42+
expectedPath = Path.Combine(expectedPath, binaryName);
3843
Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath);
3944
}
4045
[TestMethod]
4146
public void TestBinaryPathOnEmpty()
4247
{
4348
tunnel = new TunnelClass();
4449
tunnel.addBinaryPath("");
45-
string expectedPath = Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack");
46-
expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe");
50+
string expectedPath = Path.Combine(homepath, ".browserstack");
51+
expectedPath = Path.Combine(expectedPath, binaryName);
4752
Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath);
4853
}
4954
[TestMethod]
@@ -55,18 +60,18 @@ public void TestBinaryPathOnFallback()
5560
Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath);
5661

5762
tunnel.fallbackPaths();
58-
expectedPath = Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack");
59-
expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe");
63+
expectedPath = Path.Combine(homepath, ".browserstack");
64+
expectedPath = Path.Combine(expectedPath, binaryName);
6065
Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath);
6166

6267
tunnel.fallbackPaths();
6368
expectedPath = Directory.GetCurrentDirectory();
64-
expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe");
69+
expectedPath = Path.Combine(expectedPath, binaryName);
6570
Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath);
6671

6772
tunnel.fallbackPaths();
6873
expectedPath = Path.GetTempPath();
69-
expectedPath = Path.Combine(expectedPath, "BrowserStackLocal.exe");
74+
expectedPath = Path.Combine(expectedPath, binaryName);
7075
Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath);
7176
}
7277
[TestMethod]
@@ -79,7 +84,7 @@ public void TestBinaryPathOnNoMoreFallback()
7984
tunnel.fallbackPaths();
8085
Assert.Throws(typeof(Exception),
8186
new TestDelegate(testFallbackException),
82-
"Binary not found or failed to launch. Make sure that BrowserStackLocal.exe is not already running."
87+
"Binary not found or failed to launch. Make sure that BrowserStackLocal is not already running."
8388
);
8489
}
8590
[TestMethod]

BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ public enum LocalState { Idle, Connecting, Connected, Error, Disconnected };
1414

1515
public class BrowserStackTunnel : IDisposable
1616
{
17-
static readonly string binaryName = "BrowserStackLocal.exe";
18-
static readonly string downloadURL = "https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal.exe";
17+
static readonly string binaryName = isDarwin() ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal.exe";
18+
static readonly string downloadURL = isDarwin() ?
19+
"https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal-darwin-x64" :
20+
"https://bstack-local-prod.s3.amazonaws.com/BrowserStackLocal.exe";
21+
static readonly string homepath = isDarwin() ?
22+
Environment.GetFolderPath(Environment.SpecialFolder.Personal) :
23+
Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
1924
public static readonly string[] basePaths = new string[] {
20-
Path.Combine(Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"), ".browserstack"),
25+
Path.Combine(homepath, ".browserstack"),
2126
Directory.GetCurrentDirectory(),
2227
Path.GetTempPath() };
2328

@@ -32,6 +37,12 @@ public class BrowserStackTunnel : IDisposable
3237

3338
Process process = null;
3439

40+
static Boolean isDarwin()
41+
{
42+
OperatingSystem os = Environment.OSVersion;
43+
return os.Platform.ToString() == "Unix";
44+
}
45+
3546
public virtual void addBinaryPath(string binaryAbsolute)
3647
{
3748
if (binaryAbsolute == null || binaryAbsolute.Trim().Length == 0)
@@ -60,11 +71,37 @@ public virtual void fallbackPaths()
6071
{
6172
if (basePathsIndex >= basePaths.Length - 1)
6273
{
63-
throw new Exception("Binary not found or failed to launch. Make sure that BrowserStackLocal.exe is not already running.");
74+
throw new Exception("Binary not found or failed to launch. Make sure that BrowserStackLocal is not already running.");
6475
}
6576
basePathsIndex++;
6677
binaryAbsolute = Path.Combine(basePaths[basePathsIndex], binaryName);
6778
}
79+
80+
public void modifyBinaryPermission()
81+
{
82+
if (isDarwin())
83+
{
84+
try
85+
{
86+
using (Process proc = Process.Start("/bin/bash", $"-c \"chmod 0755 {this.binaryAbsolute}\""))
87+
{
88+
proc.WaitForExit();
89+
}
90+
}
91+
catch
92+
{
93+
throw new Exception("Error in changing permission for file " + this.binaryAbsolute);
94+
}
95+
}
96+
else
97+
{
98+
DirectoryInfo dInfo = new DirectoryInfo(binaryAbsolute);
99+
DirectorySecurity dSecurity = dInfo.GetAccessControl();
100+
dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
101+
dInfo.SetAccessControl(dSecurity);
102+
}
103+
}
104+
68105
public void downloadBinary()
69106
{
70107
string binaryDirectory = Path.Combine(this.binaryAbsolute, "..");
@@ -82,10 +119,7 @@ public void downloadBinary()
82119
throw new Exception("Error accessing file " + binaryAbsolute);
83120
}
84121

85-
DirectoryInfo dInfo = new DirectoryInfo(binaryAbsolute);
86-
DirectorySecurity dSecurity = dInfo.GetAccessControl();
87-
dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
88-
dInfo.SetAccessControl(dSecurity);
122+
modifyBinaryPermission();
89123
}
90124

91125
public virtual void Run(string accessKey, string folder, string logFilePath, string processType)
@@ -158,6 +192,7 @@ private void RunProcess(string arguments, string processType)
158192
else if (connectionState.ToString().ToLower().Equals("disconnected"))
159193
{
160194
SetTunnelState(LocalState.Disconnected);
195+
throw new Exception("Error while executing BrowserStackLocal " + processType + " " + e.Data);
161196
}
162197
else
163198
{

BrowserStackLocal/BrowserStackLocalIntegrationTests/IntegrationTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ namespace BrowserStackLocalIntegrationTests
1111
{
1212
public class IntegrationTests
1313
{
14+
static readonly OperatingSystem os = Environment.OSVersion;
15+
static readonly string binaryName = os.Platform.ToString() == "Unix" ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal";
1416
private static string username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
1517
private static string accesskey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
1618

1719
private List<KeyValuePair<string, string>> options = new List<KeyValuePair<string, string>>() {
1820
new KeyValuePair<string, string>("key", accesskey),
1921
new KeyValuePair<string, string>("verbose", "true"),
2022
new KeyValuePair<string, string>("forcelocal", "true"),
21-
new KeyValuePair<string, string>("binarypath", "C:\\Users\\Admin\\Desktop\\BrowserStackLocal.exe"),
22-
new KeyValuePair<string, string>("logfile", "C:\\Users\\Admin\\Desktop\\local.log"),
2323
};
2424

2525
[Test]
@@ -33,7 +33,7 @@ void startWithOptions()
3333
}
3434

3535
Assert.DoesNotThrow(new TestDelegate(startWithOptions));
36-
Process[] binaryInstances = Process.GetProcessesByName("BrowserStackLocal");
36+
Process[] binaryInstances = Process.GetProcessesByName(binaryName);
3737
Assert.AreNotEqual(binaryInstances.Length, 0);
3838

3939
IWebDriver driver;
@@ -54,7 +54,7 @@ void startWithOptions()
5454
driver.Quit();
5555
local.stop();
5656

57-
binaryInstances = Process.GetProcessesByName("BrowserStackLocal");
57+
binaryInstances = Process.GetProcessesByName(binaryName);
5858
Assert.AreEqual(binaryInstances.Length, 0);
5959
}
6060

@@ -76,7 +76,7 @@ public void TestBinaryState()
7676
[TearDown]
7777
public void TestCleanup()
7878
{
79-
foreach(Process p in Process.GetProcessesByName("BrowserStackLocal"))
79+
foreach(Process p in Process.GetProcessesByName(binaryName))
8080
{
8181
p.Kill();
8282
}

0 commit comments

Comments
 (0)