Debian Codegraph @

external/nuget-buildtasks/src/Microsoft.NuGet.Build.Tasks.Tests/Helpers/TempDirectory.cs

raw symbols SCIP
1 // Copyright (c) .NET Foundation. All rights reserved.
2 // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
4 using System;
5 using System.Diagnostics;
6 using System.IO;
7
8 namespace Microsoft.NuGet.Build.Tasks.Tests.Helpers
9 {
10 public class TempDirectory
11 {
12 private readonly string _path;
13 private readonly TempRoot _root;
14
15 protected TempDirectory(TempRoot root)
16 : this(root.Root, root)
17 {
18 }
19
20 private TempDirectory(string path, TempRoot root)
21 {
22 Debug.Assert(path != null);
23 Debug.Assert(root != null);
24
25 _path = path;
26 _root = root;
27 }
28
29 private static string CreateUniqueDirectory(string basePath)
30 {
31 while (true)
32 {
33 string dir = System.IO.Path.Combine(basePath, Guid.NewGuid().ToString());
34 try
35 {
36 Directory.CreateDirectory(dir);
37 return dir;
38 }
39 catch (IOException)
40 {
41 // retry
42 }
43 }
44 }
45
46 public string Path
47 {
48 get { return _path; }
49 }
50
51 /// <summary>
52 /// Creates a file in this directory.
53 /// </summary>
54 /// <param name="name">File name.</param>
55 public TempFile CreateFile(string name)
56 {
57 string filePath = System.IO.Path.Combine(_path, name);
58 TempRoot.CreateStream(filePath);
59 return _root.AddFile(new DisposableFile(filePath));
60 }
61
62 /// <summary>
63 /// Creates a file in this directory that is a copy of the specified file.
64 /// </summary>
65 public TempFile CopyFile(string originalPath)
66 {
67 string name = System.IO.Path.GetFileName(originalPath);
68 string filePath = System.IO.Path.Combine(_path, name);
69 File.Copy(originalPath, filePath);
70 return _root.AddFile(new DisposableFile(filePath));
71 }
72
73 /// <summary>
74 /// Creates a subdirectory in this directory.
75 /// </summary>
76 /// <param name="name">Directory name or unrooted directory path.</param>
77 public TempDirectory CreateDirectory(string name)
78 {
79 string dirPath = System.IO.Path.Combine(_path, name);
80 Directory.CreateDirectory(dirPath);
81 return new TempDirectory(dirPath, _root);
82 }
83
84 public override string ToString()
85 {
86 return _path;
87 }
88 }
89 }