|
3 | 3 | * PROJECT: FileHandler |
4 | 4 | * FILE: FileHandler/DirectoryInformation.cs |
5 | 5 | * PURPOSE: Generic System Functions for Directories |
6 | | -* PROGRAMER: Peter Geinitz (Wayfarer) |
| 6 | +* PROGRAMER: Peter Geinitz (Wayfarer) – Refactor by ChatGPT |
7 | 7 | */ |
8 | 8 |
|
9 | | -// ReSharper disable UnusedType.Global |
10 | | - |
11 | 9 | using System; |
12 | 10 | using System.Diagnostics; |
13 | 11 | using System.IO; |
14 | 12 |
|
15 | 13 | namespace FileHandler; |
16 | 14 |
|
17 | 15 | /// <summary> |
18 | | -/// The directory information class. |
| 16 | +/// Directory helper class with testable parent retrieval |
19 | 17 | /// </summary> |
20 | 18 | public static class DirectoryInformation |
21 | 19 | { |
22 | 20 | /// <summary> |
23 | | - /// Returns Path at the defined level height |
| 21 | + /// Returns a parent directory at the specified level from the current working directory. |
24 | 22 | /// </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> |
28 | 25 | 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); |
36 | 27 |
|
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"); |
38 | 40 |
|
39 | | - if (string.IsNullOrEmpty(path)) |
40 | | - { |
41 | | - throw new FileHandlerException($"{FileHandlerResources.ErrorGetParentDirectory} {path}"); |
42 | | - } |
| 41 | + var path = startPath; |
43 | 42 |
|
44 | 43 | try |
45 | 44 | { |
46 | 45 | for (var i = 0; i < level; i++) |
47 | 46 | { |
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; |
49 | 51 | } |
50 | 52 | } |
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) |
52 | 54 | { |
53 | | - FileHandlerRegister.AddError(nameof(GetParentDirectory), path, ex); |
| 55 | + FileHandlerRegister.AddError(nameof(GetParentDirectoryFromPath), path, ex); |
54 | 56 | Trace.WriteLine(ex); |
55 | | - throw new FileHandlerException($"{FileHandlerResources.ErrorGetParentDirectory} {ex}"); |
| 57 | + throw new FileHandlerException($"{FileHandlerResources.ErrorGetParentDirectory}: {ex.Message}"); |
56 | 58 | } |
57 | 59 |
|
58 | 60 | return path; |
|
0 commit comments