@@ -17,31 +17,30 @@ public void CanGetBlobAsText()
17
17
{
18
18
var blob = repo . Lookup < Blob > ( "a8233120f6ad708f843d861ce2b7228ec4e3dec6" ) ;
19
19
20
- var text = blob . ContentAsText ( ) ;
20
+ var text = blob . GetContentText ( ) ;
21
21
22
22
Assert . Equal ( "hey there\n " , text ) ;
23
23
}
24
24
}
25
25
26
- [ Fact ]
27
- public void CanGetBlobAsFilteredText ( )
26
+ [ SkippableTheory ]
27
+ [ InlineData ( "false" , "hey there\n " ) ]
28
+ [ InlineData ( "input" , "hey there\n " ) ]
29
+ [ InlineData ( "true" , "hey there\r \n " ) ]
30
+ public void CanGetBlobAsFilteredText ( string autocrlf , string expectedText )
28
31
{
29
- using ( var repo = new Repository ( BareTestRepoPath ) )
32
+ SkipIfNotSupported ( autocrlf ) ;
33
+
34
+ var path = CloneBareTestRepo ( ) ;
35
+ using ( var repo = new Repository ( path ) )
30
36
{
31
- var blob = repo . Lookup < Blob > ( "a8233120f6ad708f843d861ce2b7228ec4e3dec6" ) ;
37
+ repo . Config . Set ( "core.autocrlf" , autocrlf ) ;
32
38
33
- var text = blob . ContentAsText ( new FilteringOptions ( "foo.txt" ) ) ;
39
+ var blob = repo . Lookup < Blob > ( "a8233120f6ad708f843d861ce2b7228ec4e3dec6" ) ;
34
40
35
- ConfigurationEntry < bool > autocrlf = repo . Config . Get < bool > ( "core.autocrlf" ) ;
41
+ var text = blob . GetContentText ( new FilteringOptions ( "foo.txt" ) ) ;
36
42
37
- if ( autocrlf != null && autocrlf . Value )
38
- {
39
- Assert . Equal ( "hey there\r \n " , text ) ;
40
- }
41
- else
42
- {
43
- Assert . Equal ( "hey there\n " , text ) ;
44
- }
43
+ Assert . Equal ( expectedText , text ) ;
45
44
}
46
45
}
47
46
@@ -68,15 +67,15 @@ public void CanGetBlobAsTextWithVariousEncodings(string encodingName, int expect
68
67
var commit = repo . Commit ( "bom" , Constants . Signature , Constants . Signature ) ;
69
68
70
69
var blob = ( Blob ) commit . Tree [ bomFile ] . Target ;
71
- Assert . Equal ( expectedContentBytes , blob . Content . Length ) ;
70
+ Assert . Equal ( expectedContentBytes , blob . GetContentStream ( ) . Length ) ;
72
71
73
- var textDetected = blob . ContentAsText ( ) ;
72
+ var textDetected = blob . GetContentText ( ) ;
74
73
Assert . Equal ( content , textDetected ) ;
75
74
76
- var text = blob . ContentAsText ( encoding ) ;
75
+ var text = blob . GetContentText ( encoding ) ;
77
76
Assert . Equal ( content , text ) ;
78
77
79
- var utf7Chars = blob . ContentAsText ( Encoding . UTF7 ) . Select ( c => ( ( int ) c ) . ToString ( "X2" ) ) . ToArray ( ) ;
78
+ var utf7Chars = blob . GetContentText ( Encoding . UTF7 ) . Select ( c => ( ( int ) c ) . ToString ( "X2" ) ) . ToArray ( ) ;
80
79
Assert . Equal ( expectedUtf7Chars , string . Join ( " " , utf7Chars ) ) ;
81
80
}
82
81
}
@@ -101,56 +100,47 @@ public void CanLookUpBlob()
101
100
}
102
101
}
103
102
104
- [ Fact ]
105
- public void CanReadBlobContent ( )
106
- {
107
- using ( var repo = new Repository ( BareTestRepoPath ) )
108
- {
109
- var blob = repo . Lookup < Blob > ( "a8233120f6ad708f843d861ce2b7228ec4e3dec6" ) ;
110
- byte [ ] bytes = blob . Content ;
111
- Assert . Equal ( 10 , bytes . Length ) ;
112
-
113
- string content = Encoding . UTF8 . GetString ( bytes ) ;
114
- Assert . Equal ( "hey there\n " , content ) ;
115
- }
116
- }
117
-
118
103
[ Fact ]
119
104
public void CanReadBlobStream ( )
120
105
{
121
106
using ( var repo = new Repository ( BareTestRepoPath ) )
122
107
{
123
108
var blob = repo . Lookup < Blob > ( "a8233120f6ad708f843d861ce2b7228ec4e3dec6" ) ;
124
109
125
- using ( var tr = new StreamReader ( blob . ContentStream ( ) , Encoding . UTF8 ) )
110
+ var contentStream = blob . GetContentStream ( ) ;
111
+ Assert . Equal ( blob . Size , contentStream . Length ) ;
112
+
113
+ using ( var tr = new StreamReader ( contentStream , Encoding . UTF8 ) )
126
114
{
127
115
string content = tr . ReadToEnd ( ) ;
128
116
Assert . Equal ( "hey there\n " , content ) ;
129
117
}
130
118
}
131
119
}
132
120
133
- [ Fact ]
134
- public void CanReadBlobFilteredStream ( )
121
+ [ SkippableTheory ]
122
+ [ InlineData ( "false" , "hey there\n " ) ]
123
+ [ InlineData ( "input" , "hey there\n " ) ]
124
+ [ InlineData ( "true" , "hey there\r \n " ) ]
125
+ public void CanReadBlobFilteredStream ( string autocrlf , string expectedContent )
135
126
{
136
- using ( var repo = new Repository ( BareTestRepoPath ) )
127
+ SkipIfNotSupported ( autocrlf ) ;
128
+
129
+ var path = CloneBareTestRepo ( ) ;
130
+ using ( var repo = new Repository ( path ) )
137
131
{
132
+ repo . Config . Set ( "core.autocrlf" , autocrlf ) ;
133
+
138
134
var blob = repo . Lookup < Blob > ( "a8233120f6ad708f843d861ce2b7228ec4e3dec6" ) ;
139
135
140
- using ( var tr = new StreamReader ( blob . ContentStream ( new FilteringOptions ( "foo.txt" ) ) , Encoding . UTF8 ) )
136
+ var contentStream = blob . GetContentStream ( new FilteringOptions ( "foo.txt" ) ) ;
137
+ Assert . Equal ( expectedContent . Length , contentStream . Length ) ;
138
+
139
+ using ( var tr = new StreamReader ( contentStream , Encoding . UTF8 ) )
141
140
{
142
141
string content = tr . ReadToEnd ( ) ;
143
142
144
- ConfigurationEntry < bool > autocrlf = repo . Config . Get < bool > ( "core.autocrlf" ) ;
145
-
146
- if ( autocrlf != null && autocrlf . Value )
147
- {
148
- Assert . Equal ( "hey there\r \n " , content ) ;
149
- }
150
- else
151
- {
152
- Assert . Equal ( &q
10000
uot;hey there\n " , content ) ;
153
- }
143
+ Assert . Equal ( expectedContent , content ) ;
154
144
}
155
145
}
156
146
}
@@ -167,40 +157,15 @@ public void CanReadBlobFilteredStreamOfUnmodifiedBinary()
167
157
{
168
158
Blob blob = repo . ObjectDatabase . CreateBlob ( stream ) ;
169
159
170
- using ( var filtered = blob . ContentStream ( new FilteringOptions ( "foo.txt" ) ) )
160
+ using ( var filtered = blob . GetContentStream ( new FilteringOptions ( "foo.txt" ) ) )
171
161
{
162
+ Assert . Equal ( blob . Size , filtered . Length ) ;
172
163
Assert . True ( StreamEquals ( stream , filtered ) ) ;
173
164
}
174
165
}
175
166
}
176
167
}
177
168
178
- public static void CopyStream ( Stream input , Stream output )
179
- {
180
- // Reused from the following Stack Overflow post with permission
181
- // of Jon Skeet (obtained on 25 Feb 2013)
182
- // http://stackoverflow.com/questions/411592/how-do-i-save-a-stream-to-a-file/411605#411605
183
- var buffer = new byte [ 8 * 1024 ] ;
184
- int len ;
185
- while ( ( len = input . Read ( buffer , 0 , buffer . Length ) ) > 0 )
186
- {
187
- output . Write ( buffer , 0 , len ) ;
188
- }
189
- }
190
-
191
- public static bool StreamEquals ( Stream one , Stream two )
192
- {
193
- int onebyte , twobyte ;
194
-
195
- while ( ( onebyte = one . ReadByte ( ) ) >= 0 && ( twobyte = two . ReadByte ( ) ) >= 0 )
196
- {
197
- if ( onebyte != twobyte )
198
- return false ;
199
- }
200
-
201
- return true ;
202
- }
203
-
204
169
[ Fact ]
205
170
public void CanStageAFileGeneratedFromABlobContentStream ( )
206
171
{
@@ -224,7 +189,7 @@ public void CanStageAFileGeneratedFromABlobContentStream()
224
189
225
190
var blob = repo . Lookup < Blob > ( entry . Id . Sha ) ;
226
191
227
- using ( Stream stream = blob . ContentStream ( ) )
192
+ using ( Stream stream = blob . GetContentStream ( ) )
228
193
using ( Stream file = File . OpenWrite ( Path . Combine ( repo . Info . WorkingDirectory , "small.fromblob.txt" ) ) )
229
194
{
230
195
CopyStream ( stream , file ) ;
@@ -246,5 +211,11 @@ public void CanTellIfTheBlobContentLooksLikeBinary()
246
211
Assert . Equal ( false , blob . IsBinary ) ;
247
212
}
248
213
}
214
+
215
+ private static void SkipIfNotSupported ( string autocrlf )
216
+ {
217
+ InconclusiveIf ( ( ) => autocrlf == "true" && IsRunningOnLinux ( ) , "Non-Windows does not support core.autocrlf = true" ) ;
218
+ InconclusiveIf ( ( ) => autocrlf == "input" && ! IsRunningOnLinux ( ) , "Windows does not support core.autocrlf = input" ) ;
219
+ }
249
220
}
250
221
}
0 commit comments