diff --git a/src/dotnet-install.sh b/src/dotnet-install.sh index bd13ffa66..6e203e7be 100755 --- a/src/dotnet-install.sh +++ b/src/dotnet-install.sh @@ -198,7 +198,17 @@ get_current_os_name() { elif [ "$uname" = "FreeBSD" ]; then echo "freebsd" return 0 + elif [ "$uname" = "HarmonyOS" ] || [ "$uname" = "OpenHarmony" ] || [ "$uname" = "OHOS" ]; then + # HarmonyOS/OpenHarmony report their own uname value; the portable RID is openharmony-. + echo "openharmony" + return 0 elif [ "$uname" = "Linux" ]; then + # OpenHarmony standard systems can report Linux and may not ship /etc/os-release. + if [ -r /etc/os-release ] && grep -qiE '^(ID|ID_LIKE)="?[^"]*(openharmony|ohos)' /etc/os-release; then + echo "openharmony" + return 0 + fi + local linux_platform_name="" linux_platform_name="$(get_linux_platform_name)" || true @@ -307,7 +317,7 @@ get_machine_architecture() { return 0 ;; aarch64|arm64) - if [ "$(getconf LONG_BIT)" -lt 64 ]; then + if machine_has "getconf" && [ "$(getconf LONG_BIT 2>/dev/null || echo 64)" -lt 64 ]; then # This is 32-bit OS running on 64-bit CPU (for example Raspberry Pi OS) echo "arm" return 0 @@ -448,7 +458,7 @@ get_normalized_os() { local osname="$(to_lowercase "$1")" if [ ! -z "$osname" ]; then case "$osname" in - osx | freebsd | rhel.6 | linux-musl | linux) + osx | freebsd | rhel.6 | linux-musl | linux | openharmony) echo "$osname" return 0 ;; @@ -457,8 +467,13 @@ get_normalized_os() { echo "$osname" return 0 ;; + harmonyos | ohos) + osname='openharmony' + echo "$osname" + return 0 + ;; *) - say_err "'$user_defined_os' is not a supported value for --os option, supported values are: osx, macos, linux, linux-musl, freebsd, rhel.6. If you think this is a bug, report it at https://github.com/dotnet/install-scripts/issues." + say_err "'$user_defined_os' is not a supported value for --os option, supported values are: osx, macos, linux, linux-musl, freebsd, rhel.6, openharmony. If you think this is a bug, report it at https://github.com/dotnet/install-scripts/issues." return 1 ;; esac @@ -1807,7 +1822,7 @@ do echo " --arch,-Architecture,-Arch" echo " Possible values: x64, arm, arm64, s390x, ppc64le and loongarch64" echo " --os Specifies operating system to be used when selecting the installer." - echo " Overrides the OS determination approach used by the script. Supported values: osx, linux, linux-musl, freebsd, rhel.6." + echo " Overrides the OS determination approach used by the script. Supported values: osx, linux, linux-musl, freebsd, rhel.6, openharmony." echo " In case any other value is provided, the platform will be determined by the script based on machine configuration." echo " Not supported for legacy links. Use --runtime-id to specify platform for legacy links." echo " Refer to: https://aka.ms/dotnet-os-lifecycle for more information." diff --git a/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs b/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs new file mode 100644 index 000000000..435dc797a --- /dev/null +++ b/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs @@ -0,0 +1,144 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; +using Install_Scripts.Test.Utils; +using Xunit; + +namespace Microsoft.DotNet.InstallationScript.Tests +{ + /// + /// Tests for OpenHarmony/HarmonyOS OS detection in dotnet-install.sh. + /// Unix-only because the script under test is the bash variant. + /// + public class OpenHarmonyOsDetectionTests + { + [Fact] + public void WhenTheMachineReportsHarmonyOS_TheLinkUsesTheOpenHarmonyRid() + { + Assert.SkipWhen(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), + "dotnet-install.sh tests are Unix-only."); + + using var fixture = new OsDetectionFixture(); + + CommandResult result = fixture.Run( + "-dryrun", + "-version", "11.0.100", + "-azurefeed", "file://" + fixture.FeedPath); + + Assert.Equal(0, result.ExitCode); + Assert.Contains("dotnet-sdk-11.0.100-openharmony-arm64.tar.gz", result.StdOut); + } + + [Theory] + [InlineData("harmonyos")] + [InlineData("ohos")] + [InlineData("openharmony")] + public void WhenAnOpenHarmonyOsAliasIsPassed_TheLinkUsesTheOpenHarmonyRid(string osAlias) + { + Assert.SkipWhen(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), + "dotnet-install.sh tests are Unix-only."); + + using var fixture = new OsDetectionFixture(); + + CommandResult result = fixture.Run( + "-dryrun", + "-version", "11.0.100", + "--os", osAlias, + "-azurefeed", "file://" + fixture.FeedPath); + + Assert.Equal(0, result.ExitCode); + Assert.Contains("openharmony-arm64", result.StdOut); + } + + private sealed class OsDetectionFixture : IDisposable + { + private readonly string _root; + private readonly string _shimDir; + + public OsDetectionFixture() + { + _root = Path.Combine(Path.GetTempPath(), "dotnet-ohos-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(_root); + + // Override uname so the script sees HarmonyOS regardless of the test host. + _shimDir = Path.Combine(_root, "shim"); + Directory.CreateDirectory(_shimDir); + string unamePath = Path.Combine(_shimDir, "uname"); + File.WriteAllText(unamePath, "#!/bin/sh\ncase \"${1:-}\" in\n -m) echo aarch64 ;;\n -s) echo HarmonyOS ;;\n *) echo HarmonyOS ;;\nesac\n"); + // CA1416: SetUnixFileMode is Unix-only. The tests are skipped on Windows; the + // guard keeps the analyzer (and a future Windows run) happy. + if (!OperatingSystem.IsWindows()) + { + File.SetUnixFileMode(unamePath, + UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute | + UnixFileMode.GroupRead | UnixFileMode.GroupExecute | + UnixFileMode.OtherRead | UnixFileMode.OtherExecute); + } + + FeedPath = Path.Combine(_root, "feed"); + string versionDir = Path.Combine(FeedPath, "Sdk", "11.0.100"); + Directory.CreateDirectory(versionDir); + File.WriteAllText(Path.Combine(versionDir, "productVersion.txt"), "11.0.100\n"); + } + + public string FeedPath { get; } + + public CommandResult Run(params string[] args) + { + var startInfo = new ProcessStartInfo + { + FileName = "/bin/bash", + Arguments = $"{GetScriptPath()} {ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args)}", + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + startInfo.Environment["PATH"] = _shimDir + Path.PathSeparator + Environment.GetEnvironmentVariable("PATH"); + startInfo.Environment["TMPDIR"] = _root; + + using Process process = Process.Start(startInfo)!; + string stdout = process.StandardOutput.ReadToEnd(); + string stderr = process.StandardError.ReadToEnd(); + process.WaitForExit(); + + return new CommandResult(process.ExitCode, stdout, stderr); + } + + private static string GetScriptPath() + { + string? directory = AppContext.BaseDirectory; + while (directory != null && !Directory.Exists(Path.Combine(directory, ".git"))) + { + directory = Directory.GetParent(directory)?.FullName; + } + + if (directory == null) + { + throw new InvalidOperationException("Could not locate the repository root."); + } + + return Path.Combine(directory, "src", "dotnet-install.sh"); + } + + public void Dispose() + { + try + { + Directory.Delete(_root, true); + } + catch + { + // Best effort cleanup. + } + } + } + + private sealed record CommandResult(int ExitCode, string StdOut, string StdErr); + } +}