Skip to content

Commit eaf35be

Browse files
author
LoneWandererProductions
committed
fix up another test and another function
1 parent 9d10d09 commit eaf35be

2 files changed

Lines changed: 46 additions & 44 deletions

File tree

CommonLibraryTests/IoFileSearch.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -146,36 +146,36 @@ public async Task GetFilesByExtensionWithoutExtensionAsync()
146146
}
147147

148148
/// <summary>
149-
/// Simple Check for getting files Contains in a Folder
149+
/// Simple Check for getting files contained in a folder.
150150
/// </summary>
151151
[TestMethod]
152-
public void GetAllSubfolders()
152+
public void GetAllSubfoldersDeterministic()
153153
{
154-
var check = false;
155-
var list = FileHandleSearch.GetAllSubfolders(Directory.GetCurrentDirectory());
154+
// Use a known path for testing (deterministic)
155+
var currentDir = Directory.GetCurrentDirectory();
156156

157-
if (list == null)
158-
{
159-
Assert.Fail("Null Reference");
160-
}
157+
// Navigate to the project folder (assume tests are in bin\Debug\netX.Y)
158+
var projectRoot = Directory.GetParent(currentDir)?.Parent?.Parent?.FullName;
159+
Assert.IsNotNull(projectRoot, "Could not determine project root directory.");
161160

162-
if (list.Count > 0)
163-
{
164-
check = true;
165-
}
161+
// Retrieve all subfolders using your method
162+
var list = FileHandleSearch.GetAllSubfolders(projectRoot);
166163

167-
Assert.IsTrue(check, "Did not get all Folders");
164+
Assert.IsNotNull(list, "GetAllSubfolders returned null.");
165+
Assert.IsTrue(list.Count > 0, "Did not get any subfolders.");
168166

169-
var path = DirectoryInformation.GetParentDirectory(1);
170-
Assert.IsTrue(path.EndsWith("\\CoreLibrary\\CommonLibraryTests\\bin", StringComparison.Ordinal),
171-
$"Wrong Directory Name: {path}");
167+
// Test DirectoryInformation.GetParentDirectory deterministically
168+
// Use projectRoot as starting point
169+
var parent1 = DirectoryInformation.GetParentDirectoryFromPath(projectRoot, 1);
170+
Assert.IsTrue(parent1.EndsWith("\\CoreLibrary", StringComparison.Ordinal),
171+
$"Wrong Directory Name: {parent1}");
172172

173-
path = DirectoryInformation.GetParentDirectory(2);
174-
175-
Assert.IsTrue(path.EndsWith("\\CoreLibrary\\CommonLibraryTests", StringComparison.Ordinal),
176-
$"Wrong Directory Name: {path}");
173+
var parent2 = DirectoryInformation.GetParentDirectoryFromPath(projectRoot, 2);
174+
Assert.IsTrue(parent2.EndsWith("\\Source", StringComparison.Ordinal),
175+
$"Wrong Directory Name: {parent2}");
177176
}
178177

178+
179179
/// <summary>
180180
/// Gets the file information.
181181
/// </summary>

FileHandler/DirectoryInformation.cs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,58 @@
33
* PROJECT: FileHandler
44
* FILE: FileHandler/DirectoryInformation.cs
55
* PURPOSE: Generic System Functions for Directories
6-
* PROGRAMER: Peter Geinitz (Wayfarer)
6+
* PROGRAMER: Peter Geinitz (Wayfarer) – Refactor by ChatGPT
77
*/
88

9-
// ReSharper disable UnusedType.Global
10-
119
using System;
1210
using System.Diagnostics;
1311
using System.IO;
1412

1513
namespace FileHandler;
1614

1715
/// <summary>
18-
/// The directory information class.
16+
/// Directory helper class with testable parent retrieval
1917
/// </summary>
2018
public static class DirectoryInformation
2119
{
2220
/// <summary>
23-
/// Returns Path at the defined level height
21+
/// Returns a parent directory at the specified level from the current working directory.
2422
/// </summary>
25-
/// <param name="level">Height of Path</param>
26-
/// <returns>Returns Path at the current height, can return null.</returns>
27-
/// <exception cref="FileHandlerException">No Correct Path was provided</exception>
23+
/// <param name="level">Levels to go up</param>
24+
/// <returns>Parent directory path</returns>
2825
public static string GetParentDirectory(int level)
29-
{
30-
var root = Directory.GetCurrentDirectory();
31-
32-
if (string.IsNullOrEmpty(root))
33-
{
34-
throw new FileHandlerException($"{FileHandlerResources.ErrorGetParentDirectory} {root}");
35-
}
26+
=> GetParentDirectoryFromPath(Directory.GetCurrentDirectory(), level);
3627

37-
var path = Directory.GetParent(root)?.ToString();
28+
/// <summary>
29+
/// Returns a parent directory at the specified level from a given path.
30+
/// This method is safe for unit testing with arbitrary paths.
31+
/// </summary>
32+
/// <param name="startPath">The path to start from</param>
33+
/// <param name="level">Levels to go up</param>
34+
/// <returns>Parent directory path</returns>
35+
/// <exception cref="FileHandlerException"></exception>
36+
public static string GetParentDirectoryFromPath(string startPath, int level)
37+
{
38+
if (string.IsNullOrEmpty(startPath))
39+
throw new FileHandlerException($"{FileHandlerResources.ErrorGetParentDirectory}: startPath was empty");
3840

39-
if (string.IsNullOrEmpty(path))
40-
{
41-
throw new FileHandlerException($"{FileHandlerResources.ErrorGetParentDirectory} {path}");
42-
}
41+
var path = startPath;
4342

4443
try
4544
{
4645
for (var i = 0; i < level; i++)
4746
{
48-
path = Directory.GetParent(path!)?.ToString();
47+
var parent = Directory.GetParent(path);
48+
if (parent == null)
49+
throw new FileHandlerException($"{FileHandlerResources.ErrorGetParentDirectory}: reached root at level {i}");
50+
path = parent.FullName;
4951
}
5052
}
51-
catch (Exception ex) when (ex is UnauthorizedAccessException or DirectoryNotFoundException or IOException)
53+
catch (Exception ex) when (ex is UnauthorizedAccessException or DirectoryNotFoundException or IOException or FileHandlerException)
5254
{
53-
FileHandlerRegister.AddError(nameof(GetParentDirectory), path, ex);
55+
FileHandlerRegister.AddError(nameof(GetParentDirectoryFromPath), path, ex);
5456
Trace.WriteLine(ex);
55-
throw new FileHandlerException($"{FileHandlerResources.ErrorGetParentDirectory} {ex}");
57+
throw new FileHandlerException($"{FileHandlerResources.ErrorGetParentDirectory}: {ex.Message}");
5658
}
5759

5860
return path;

0 commit comments

Comments
 (0)