-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Update Node path to compile with appium 2 and make him cross-pla…
…tform (#659) * fix: Update WindowsNode path to compile with appium 2 refactor: Move Appium.js path retrieval to helpers.paths class * refactor!: Retrieve npm prefix path and use cross-platform commands to get appium script * fix: npm install prefix and npm install path mixup * fix: make PathToNode compatible with both Windows and Unix-based systems * chore: Remove redundant NodePaths from the resources * chore: Remove comment section * chore: Add exception if npm path cannot be detected * chore: Rename _pathToCustomizedAppiumJs * chore: Add timeout for RunCommand chore: Remove private var envPlatform * chore: Optimize code performance * chore: Optimize path readability on Windows platforms * chore: Switch from `npm list -g --depth=0` to `npm -g root` for better performance * chore: Remove comment section * chore: Throw specific exception for npm commands * chore: Move declaration near reference * chore: Rename GetAppiumJsPath * chore: Improve NPM path not found exception * chore: Rename resource file name * chore: Remove path replacement and add XML DOC * chore: Verify exit code on RunCommand() from Npm * chore: Use GetNpmExecutablePath() on all platforms * chore: log command upon errorin RunCommand() * chore: Improve NpmNotFoundException log * chore: Rename GetAppiumPackageIndexPath() * chore: Simplify InitAppiumPackageIndexPath() * chore: Simplify paths creation * chore: More exceptions mapping for RunCommand * fix: More exceptions improvements in RunCommand() * fix: Exceptions makeover pt. 3 * fix: npm typo * chore: Minimise try-catch * chore: Remove unnecessary using
- Loading branch information
Showing
10 changed files
with
178 additions
and
74 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
|
||
namespace Appium.Net.Integration.Tests.helpers | ||
{ | ||
internal class Npm | ||
{ | ||
|
||
public static string GetNpmPrefixPath() | ||
{ | ||
string npmPath = GetNpmExecutablePath(); | ||
string npmPrefixPath = RunCommand(npmPath, "-g root"); | ||
|
||
return npmPrefixPath.Trim(); | ||
} | ||
|
||
private static string RunCommand(string command, string arguments, int timeoutMilliseconds = 30000) | ||
{ | ||
int exitCode; | ||
string output; | ||
try | ||
{ | ||
using (Process process = new Process | ||
{ | ||
StartInfo = new ProcessStartInfo | ||
{ | ||
FileName = command, | ||
Arguments = arguments, | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true, | ||
UseShellExecute = false, | ||
CreateNoWindow = true, | ||
} | ||
}) | ||
{ | ||
process.Start(); | ||
|
||
output = process.StandardOutput.ReadToEnd(); | ||
string errorOutput = process.StandardError.ReadToEnd(); | ||
_ = process.WaitForExit(timeoutMilliseconds); | ||
|
||
exitCode = process.ExitCode; | ||
} | ||
if ((exitCode == 1) && command.Contains("npm")) | ||
{ | ||
Console.WriteLine($"npm Error upon command: `{arguments}`. {output}"); | ||
throw new NpmUnknownCommandException($"Command: `{arguments}` exited with code {exitCode}. Error: {output}"); | ||
} | ||
|
||
return output; | ||
} | ||
|
||
catch (Win32Exception ex) when (command.Contains("npm")) | ||
{ | ||
Console.WriteLine(ex.Message); | ||
throw new NpmNotFoundException($"npm not found under {command}", ex); | ||
} | ||
} | ||
|
||
private static string GetNpmExecutablePath() | ||
{ | ||
string commandName = IsWindows() ? "where" : "which"; | ||
string result = RunCommand(commandName, "npm"); | ||
|
||
string npmPath; | ||
|
||
string[] lines = result?.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); | ||
|
||
if (IsWindows()) | ||
{ | ||
npmPath = lines?.FirstOrDefault(line => !string.IsNullOrWhiteSpace(line) && line.EndsWith("npm.cmd")); | ||
} | ||
else | ||
{ | ||
npmPath = lines?.FirstOrDefault(line => !string.IsNullOrWhiteSpace(line)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(npmPath)) | ||
{ | ||
throw new NpmNotFoundException("NPM executable not found. Please make sure the NPM executable is installed and check the configured PATH environment variable."); | ||
} | ||
|
||
return npmPath; | ||
} | ||
|
||
private static bool IsWindows() | ||
{ | ||
return Environment.OSVersion.Platform == PlatformID.Win32NT; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
|
||
namespace Appium.Net.Integration.Tests.helpers | ||
{ | ||
public class NpmNotFoundException : Exception | ||
{ | ||
public NpmNotFoundException() | ||
: base("Node Package Manager (npm) cannot be found. Make sure Node.js is installed and present in PATH.") | ||
{ | ||
} | ||
|
||
public NpmNotFoundException(string message) | ||
: base(message) | ||
{ | ||
} | ||
|
||
public NpmNotFoundException(string message, Exception innerException) | ||
: base(message, innerException) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
|
||
namespace Appium.Net.Integration.Tests.helpers | ||
{ | ||
public class NpmUnknownCommandException : Exception | ||
{ | ||
public NpmUnknownCommandException() | ||
: base("Unknown npm command encountered. ") | ||
{ | ||
} | ||
|
||
public NpmUnknownCommandException(string message) | ||
: base(message) | ||
{ | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Appium.Net.Integration.Tests.helpers; | ||
using System.IO; | ||
|
||
namespace Appium.Net.Integration.Tests.Helpers | ||
{ | ||
internal class Paths | ||
{ | ||
private string _pathToAppiumPackageIndex; | ||
|
||
public string PathToAppiumPackageIndex | ||
{ | ||
get | ||
{ | ||
if (_pathToAppiumPackageIndex == null) | ||
{ | ||
InitAppiumPackageIndexPath(); | ||
} | ||
return _pathToAppiumPackageIndex; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Initializes the Appium package index path by combining the components "appium" and "index.js" with the npm prefix path. | ||
/// </summary> | ||
/// <remarks> | ||
/// This method sets the _pathToAppiumPackageIndex variable by combining the specified components with the npm prefix path. | ||
/// </remarks> | ||
private void InitAppiumPackageIndexPath() | ||
{ | ||
string[] appiumJsPathComponents = { "appium", "index.js" }; | ||
string npmPath = Npm.GetNpmPrefixPath(); | ||
|
||
_pathToAppiumPackageIndex = Path.Combine(npmPath, Path.Combine(appiumJsPathComponents)); | ||
} | ||
} | ||
} |