Skip to content

Commit

Permalink
Add commandline arg -testHelperScreenshotDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
nowsprinting committed Mar 21, 2024
1 parent 0ec8626 commit 915be96
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 12 deletions.
12 changes: 8 additions & 4 deletions Runtime/Attributes/TakeScreenshotAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -26,7 +26,7 @@ public class TakeScreenshotAttribute : NUnitAttribute, IOuterUnityTestAction
/// <summary>
/// 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 <c>ScreenshotHelper</c> class.
/// If you want to take screenshots at any time, use the <c>TestHelper.RuntimeInternals.ScreenshotHelper</c> class.
/// </summary>
/// <remarks>
/// Limitations:
Expand All @@ -35,8 +35,12 @@ public class TakeScreenshotAttribute : NUnitAttribute, IOuterUnityTestAction
/// <br/>
/// Using <c>ScreenCapture.CaptureScreenshotAsTexture</c> internally.
/// </remarks>
/// <param name="directory">Directory to save screenshots.</param>
/// <param name="filename">Filename to store screenshot.</param>
/// <param name="directory">Directory to save screenshots.
/// If omitted, the directory specified by command line argument "-testHelperScreenshotDirectory" is used.
/// If the command line argument is also omitted, <c>Application.persistentDataPath</c> + "/TestHelper/Screenshots/" is used.</param>
/// <param name="filename">Filename to store screenshot.
/// Default filename is <c>CurrentTest.Name</c> + ".png" when run in test-framework context.
/// Using caller method name when run in runtime context.</param>
/// <param name="superSize">The factor to increase resolution with.</param>
/// <param name="stereoCaptureMode">The eye texture to capture when stereo rendering is enabled.</param>
/// <param name="gizmos">True: show Gizmos on GameView</param>
Expand Down
38 changes: 38 additions & 0 deletions RuntimeInternals/CommandLineArgs.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Manage command line arguments for test-helper.
/// </summary>
public static class CommandLineArgs
{
internal static string[] CachedCommandLineArgs = Environment.GetCommandLineArgs();

/// <summary>
/// Screenshot save directory.
/// Returns <c>Application.persistentDataPath</c> + "/TestHelper/Screenshots/" if not specified.
/// </summary>
/// <returns></returns>
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");
}
}
}
}
3 changes: 3 additions & 0 deletions RuntimeInternals/CommandLineArgs.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions RuntimeInternals/ScreenshotHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,10 +17,7 @@ namespace TestHelper.RuntimeInternals
/// </summary>
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)
{
Expand Down Expand Up @@ -50,7 +46,9 @@ private static string DefaultFilename(string callerMemberName)
/// <br/>
/// Using <c>ScreenCapture.CaptureScreenshotAsTexture</c> internally.
/// </remarks>
/// <param name="directory">Directory to save screenshots. Default save path is <c>Application.persistentDataPath</c> + "/TestHelper/Screenshots/".</param>
/// <param name="directory">Directory to save screenshots.
/// If omitted, the directory specified by command line argument "-testHelperScreenshotDirectory" is used.
/// If the command line argument is also omitted, <c>Application.persistentDataPath</c> + "/TestHelper/Screenshots/" is used.</param>
/// <param name="filename">Filename to store screenshot.
/// Default filename is <c>CurrentTest.Name</c> + ".png" when run in test-framework context.
/// Using caller method name when run in runtime context.</param>
Expand All @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions Tests/RuntimeInternals/CommandLineArgsTest.cs
Original file line number Diff line number Diff line change
@@ -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<string>();
var actual = CommandLineArgs.GetScreenshotDirectory();
Assert.That(actual, Is.EqualTo(Path.Combine(Application.persistentDataPath, "TestHelper", "Screenshots")));
}
}
}
3 changes: 3 additions & 0 deletions Tests/RuntimeInternals/CommandLineArgsTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 915be96

Please sign in to comment.