diff --git a/Runtime/Attributes/TakeScreenshotAttribute.cs b/Runtime/Attributes/TakeScreenshotAttribute.cs index 7dae741..9548023 100644 --- a/Runtime/Attributes/TakeScreenshotAttribute.cs +++ b/Runtime/Attributes/TakeScreenshotAttribute.cs @@ -1,4 +1,4 @@ -// Copyright (c) 2023 Koji Hasegawa. +// Copyright (c) 2023-2024 Koji Hasegawa. // This software is released under the MIT License. using System; @@ -26,7 +26,7 @@ public class TakeScreenshotAttribute : NUnitAttribute, IOuterUnityTestAction /// /// Take a screenshot and save it to file after running the test. /// Default save path is $"{Application.persistentDataPath}/TestHelper/Screenshots/{CurrentTest.Name}.png". - /// If you want to take screenshots at any time, use the ScreenshotHelper class. + /// If you want to take screenshots at any time, use the TestHelper.RuntimeInternals.ScreenshotHelper class. /// /// /// Limitations: @@ -35,8 +35,12 @@ public class TakeScreenshotAttribute : NUnitAttribute, IOuterUnityTestAction ///
/// Using ScreenCapture.CaptureScreenshotAsTexture internally. ///
- /// Directory to save screenshots. - /// Filename to store screenshot. + /// Directory to save screenshots. + /// If omitted, the directory specified by command line argument "-testHelperScreenshotDirectory" is used. + /// If the command line argument is also omitted, Application.persistentDataPath + "/TestHelper/Screenshots/" is used. + /// Filename to store screenshot. + /// Default filename is CurrentTest.Name + ".png" when run in test-framework context. + /// Using caller method name when run in runtime context. /// The factor to increase resolution with. /// The eye texture to capture when stereo rendering is enabled. /// True: show Gizmos on GameView diff --git a/RuntimeInternals/CommandLineArgs.cs b/RuntimeInternals/CommandLineArgs.cs new file mode 100644 index 0000000..4fd8077 --- /dev/null +++ b/RuntimeInternals/CommandLineArgs.cs @@ -0,0 +1,38 @@ +// Copyright (c) 2023-2024 Koji Hasegawa. +// This software is released under the MIT License. + +using System; +using System.IO; +using System.Linq; +using UnityEngine; + +namespace TestHelper.RuntimeInternals +{ + /// + /// Manage command line arguments for test-helper. + /// + public static class CommandLineArgs + { + internal static string[] CachedCommandLineArgs = Environment.GetCommandLineArgs(); + + /// + /// Screenshot save directory. + /// Returns Application.persistentDataPath + "/TestHelper/Screenshots/" if not specified. + /// + /// + public static string GetScreenshotDirectory() + { + const string ScreenshotDirectoryKey = "-testHelperScreenshotDirectory="; + + var arg = CachedCommandLineArgs.FirstOrDefault(x => x.StartsWith(ScreenshotDirectoryKey)); + if (arg != null) + { + return arg[ScreenshotDirectoryKey.Length..]; + } + else + { + return Path.Combine(Application.persistentDataPath, "TestHelper", "Screenshots"); + } + } + } +} diff --git a/RuntimeInternals/CommandLineArgs.cs.meta b/RuntimeInternals/CommandLineArgs.cs.meta new file mode 100644 index 0000000..d83f840 --- /dev/null +++ b/RuntimeInternals/CommandLineArgs.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 75d1e341a0f34380b43ef39e7e2acde6 +timeCreated: 1711029589 \ No newline at end of file diff --git a/RuntimeInternals/ScreenshotHelper.cs b/RuntimeInternals/ScreenshotHelper.cs index e7a7f0b..dd0462b 100644 --- a/RuntimeInternals/ScreenshotHelper.cs +++ b/RuntimeInternals/ScreenshotHelper.cs @@ -1,10 +1,9 @@ -// Copyright (c) 2023 Koji Hasegawa. +// Copyright (c) 2023-2024 Koji Hasegawa. // This software is released under the MIT License. using System.Collections; using System.IO; using System.Runtime.CompilerServices; -using System.Threading; using UnityEngine; #if UNITY_INCLUDE_TESTS using NUnit.Framework; @@ -18,10 +17,7 @@ namespace TestHelper.RuntimeInternals /// public static class ScreenshotHelper { - private static string DefaultDirectoryPath() - { - return Path.Combine(Application.persistentDataPath, "TestHelper", "Screenshots"); - } + private static string DefaultDirectoryPath => CommandLineArgs.GetScreenshotDirectory(); private static string DefaultFilename(string callerMemberName) { @@ -50,7 +46,9 @@ private static string DefaultFilename(string callerMemberName) ///
/// Using ScreenCapture.CaptureScreenshotAsTexture internally. /// - /// Directory to save screenshots. Default save path is Application.persistentDataPath + "/TestHelper/Screenshots/". + /// Directory to save screenshots. + /// If omitted, the directory specified by command line argument "-testHelperScreenshotDirectory" is used. + /// If the command line argument is also omitted, Application.persistentDataPath + "/TestHelper/Screenshots/" is used. /// Filename to store screenshot. /// Default filename is CurrentTest.Name + ".png" when run in test-framework context. /// Using caller method name when run in runtime context. @@ -76,7 +74,7 @@ private static string DefaultFilename(string callerMemberName) } else { - directory = DefaultDirectoryPath(); // Not apply specific directory when running on player + directory = DefaultDirectoryPath; // Not apply specific directory when running on player } Directory.CreateDirectory(directory); diff --git a/Tests/RuntimeInternals/CommandLineArgsTest.cs b/Tests/RuntimeInternals/CommandLineArgsTest.cs new file mode 100644 index 0000000..28d2e24 --- /dev/null +++ b/Tests/RuntimeInternals/CommandLineArgsTest.cs @@ -0,0 +1,36 @@ +// Copyright (c) 2023-2024 Koji Hasegawa. +// This software is released under the MIT License. + +using System; +using System.IO; +using NUnit.Framework; +using UnityEngine; + +namespace TestHelper.RuntimeInternals +{ + [TestFixture] + public class CommandLineArgsTest + { + [TearDown] + public void TearDown() + { + CommandLineArgs.CachedCommandLineArgs = Environment.GetCommandLineArgs(); + } + + [Test] + public void GetScreenshotDirectory_WithArgument_GotSpecifiedDirectory() + { + CommandLineArgs.CachedCommandLineArgs = new[] { "-testHelperScreenshotDirectory=Test" }; + var actual = CommandLineArgs.GetScreenshotDirectory(); + Assert.That(actual, Is.EqualTo("Test")); + } + + [Test] + public void GetScreenshotDirectory_WithoutArgument_GotDefaultDirectory() + { + CommandLineArgs.CachedCommandLineArgs = Array.Empty(); + var actual = CommandLineArgs.GetScreenshotDirectory(); + Assert.That(actual, Is.EqualTo(Path.Combine(Application.persistentDataPath, "TestHelper", "Screenshots"))); + } + } +} diff --git a/Tests/RuntimeInternals/CommandLineArgsTest.cs.meta b/Tests/RuntimeInternals/CommandLineArgsTest.cs.meta new file mode 100644 index 0000000..41f140d --- /dev/null +++ b/Tests/RuntimeInternals/CommandLineArgsTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d5ed4cac644d453f86e05caef8008e79 +timeCreated: 1711031297 \ No newline at end of file