-
Notifications
You must be signed in to change notification settings - Fork 585
Expand file tree
/
Copy pathTestAssets.cs
More file actions
140 lines (117 loc) · 4.27 KB
/
TestAssets.cs
File metadata and controls
140 lines (117 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Linq;
using System.Reflection;
namespace DocumentFormat.OpenXml.Tests
{
public static partial class TestAssets
{
/// <summary>
/// Open a file in memory for a test stream
/// </summary>
/// <param name="name">Name of embedded resource</param>
/// <param name="isEditable">Whether resulting stream should be editable</param>
/// <returns></returns>
public static IFile OpenFile(string name, bool isEditable)
{
if (isEditable)
{
return new MemoryFile(GetStream(name).AsMemoryStream(), name);
}
else
{
return new MemoryFile(GetStream(name), name);
}
}
/// <summary>
/// Open a test file as an actual file on disk. Must be disposed to delete file
/// </summary>
/// <param name="name">Name of embedded resource</param>
/// <param name="access">FileAccess type for file</param>
/// <returns></returns>
public static IFile OpenFile(string name, FileAccess access)
{
return new CopiedFile(GetStream(name), Path.GetExtension(name), access);
}
/// <summary>
/// Gets a readonly stream for a test file
/// </summary>
/// <param name="name">Name of embedded resource</param>
/// <returns></returns>
public static Stream GetStream(string name)
{
var assembly = typeof(TestFiles).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream($"DocumentFormat.OpenXml.Tests.Assets.assets.{name}");
var names = assembly.GetManifestResourceNames().OrderBy(t => t).ToList();
if (stream is null)
{
throw new InvalidOperationException($"Could not find stream '{name}' for test");
}
return stream;
}
/// <summary>
/// Gets a stream for a test file
/// </summary>
/// <param name="name">Name of embedded resource</param>
/// <param name="isEditable">Whether resulting stream should be editable</param>
/// <returns></returns>
public static Stream GetStream(string name, bool isEditable)
{
var stream = GetStream(name);
return isEditable ? stream.AsMemoryStream() : stream;
}
private static Stream AsMemoryStream(this Stream stream)
{
if (stream is MemoryStream ms)
{
return ms;
}
else
{
using (stream)
{
var result = new MemoryStream();
stream.CopyTo(result);
return result;
}
}
}
private class MemoryFile : IFile
{
private readonly Stream _stream;
public MemoryFile(Stream stream, string path)
{
Path = path;
_stream = stream;
}
public string Path { get; }
public bool IsEditable => _stream.CanWrite;
public FileAccess Access => _stream.CanWrite ? FileAccess.ReadWrite : FileAccess.Read;
public Stream Open() => _stream;
public void Dispose()
{
_stream.Dispose();
}
}
private class CopiedFile : IFile
{
public CopiedFile(Stream stream, string extension, FileAccess access)
{
Path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"{Guid.NewGuid()}{extension}");
Access = access;
using var fs = File.OpenWrite(Path);
stream.CopyTo(fs);
}
public string Path { get; }
public bool IsEditable => Access == FileAccess.ReadWrite;
public FileAccess Access { get; }
public Stream Open() => File.Open(Path, FileMode.Open, Access);
public void Dispose()
{
File.Delete(Path);
}
}
}
}