@@ -62,14 +62,7 @@ public void GivenNoInstalledPackagesItPrintsEmptyTable()
6262
6363 command . Execute ( ) . Should ( ) . Be ( 0 ) ;
6464
65- _reporter . Lines . Should ( ) . Equal (
66- string . Format (
67- "{0} {1} {2}" ,
68- LocalizableStrings . PackageIdColumn ,
69- LocalizableStrings . VersionColumn ,
70- LocalizableStrings . CommandsColumn
71- ) ,
72- "-------------------------------------" ) ;
65+ _reporter . Lines . Should ( ) . Equal ( EnumerateExpectedTableLines ( store . Object ) ) ;
7366 }
7467
7568 [ Fact ]
@@ -92,15 +85,7 @@ public void GivenASingleInstalledPackageItPrintsThePackage()
9285
9386 command . Execute ( ) . Should ( ) . Be ( 0 ) ;
9487
95- _reporter . Lines . Should ( ) . Equal (
96- string . Format (
97- "{0} {1} {2}" ,
98- LocalizableStrings . PackageIdColumn ,
99- LocalizableStrings . VersionColumn ,
100- LocalizableStrings . CommandsColumn
101- ) ,
102- "-------------------------------------------" ,
103- "test.tool 1.3.5-preview foo " ) ;
88+ _reporter . Lines . Should ( ) . Equal ( EnumerateExpectedTableLines ( store . Object ) ) ;
10489 }
10590
10691 [ Fact ]
@@ -137,17 +122,7 @@ public void GivenMultipleInstalledPackagesItPrintsThePackages()
137122
138123 command . Execute ( ) . Should ( ) . Be ( 0 ) ;
139124
140- _reporter . Lines . Should ( ) . Equal (
141- string . Format (
142- "{0} {1} {2} " ,
143- LocalizableStrings . PackageIdColumn ,
144- LocalizableStrings . VersionColumn ,
145- LocalizableStrings . CommandsColumn
146- ) ,
147- "----------------------------------------------" ,
148- "another.tool 2.7.3 bar " ,
149- "some.tool 1.0.0 fancy-foo" ,
150- "test.tool 1.3.5-preview foo " ) ;
125+ _reporter . Lines . Should ( ) . Equal ( EnumerateExpectedTableLines ( store . Object ) ) ;
151126 }
152127
153128 [ Fact ]
@@ -172,15 +147,7 @@ public void GivenAPackageWithMultipleCommandsItListsThem()
172147
173148 command . Execute ( ) . Should ( ) . Be ( 0 ) ;
174149
175- _reporter . Lines . Should ( ) . Equal (
176- string . Format (
177- "{0} {1} {2} " ,
178- LocalizableStrings . PackageIdColumn ,
179- LocalizableStrings . VersionColumn ,
180- LocalizableStrings . CommandsColumn
181- ) ,
182- "------------------------------------------------" ,
183- "test.tool 1.3.5-preview foo, bar, baz" ) ;
150+ _reporter . Lines . Should ( ) . Equal ( EnumerateExpectedTableLines ( store . Object ) ) ;
184151 }
185152
186153 [ Fact ]
@@ -212,20 +179,11 @@ public void GivenABrokenPackageItPrintsWarning()
212179 command . Execute ( ) . Should ( ) . Be ( 0 ) ;
213180
214181 _reporter . Lines . Should ( ) . Equal (
215- string . Format (
216- LocalizableStrings . InvalidPackageWarning ,
217- "another.tool" ,
218- "broken"
219- ) . Yellow ( ) ,
220- string . Format (
221- "{0} {1} {2} " ,
222- LocalizableStrings . PackageIdColumn ,
223- LocalizableStrings . VersionColumn ,
224- LocalizableStrings . CommandsColumn
225- ) ,
226- "--------------------------------------------" ,
227- "some.tool 1.0.0 fancy-foo" ,
228- "test.tool 1.3.5-preview foo " ) ;
182+ EnumerateExpectedTableLines ( store . Object ) . Prepend (
183+ string . Format (
184+ LocalizableStrings . InvalidPackageWarning ,
185+ "another.tool" ,
186+ "broken" ) . Yellow ( ) ) ) ;
229187 }
230188
231189 private IToolPackage CreateMockToolPackage ( string id , string version , IReadOnlyList < CommandSettings > commands )
@@ -257,5 +215,61 @@ private ListToolCommand CreateCommand(IToolPackageStore store, string options =
257215 store ,
258216 _reporter ) ;
259217 }
218+
219+ private IEnumerable < string > EnumerateExpectedTableLines ( IToolPackageStore store )
220+ {
221+ string GetCommandsString ( IToolPackage package )
222+ {
223+ return string . Join ( ListToolCommand . CommandDelimiter , package . Commands . Select ( c => c . Name ) ) ;
224+ }
225+
226+ var packages = store . EnumeratePackages ( ) . Where ( PackageHasCommands ) . OrderBy ( package => package . Id ) ;
227+ var columnDelimiter = PrintableTable < IToolPackageStore > . ColumnDelimiter ;
228+
229+ int packageIdColumnWidth = LocalizableStrings . PackageIdColumn . Length ;
230+ int versionColumnWidth = LocalizableStrings . VersionColumn . Length ;
231+ int commandsColumnWidth = LocalizableStrings . CommandsColumn . Length ;
232+ foreach ( var package in packages )
233+ {
234+ packageIdColumnWidth = Math . Max ( packageIdColumnWidth , package . Id . ToString ( ) . Length ) ;
235+ versionColumnWidth = Math . Max ( versionColumnWidth , package . Version . ToNormalizedString ( ) . Length ) ;
236+ commandsColumnWidth = Math . Max ( commandsColumnWidth , GetCommandsString ( package ) . Length ) ;
237+ }
238+
239+ yield return string . Format (
240+ "{0}{1}{2}{3}{4}" ,
241+ LocalizableStrings . PackageIdColumn . PadRight ( packageIdColumnWidth ) ,
242+ columnDelimiter ,
243+ LocalizableStrings . VersionColumn . PadRight ( versionColumnWidth ) ,
244+ columnDelimiter ,
245+ LocalizableStrings . CommandsColumn . PadRight ( commandsColumnWidth ) ) ;
246+
247+ yield return new string (
248+ '-' ,
249+ packageIdColumnWidth + versionColumnWidth + commandsColumnWidth + ( columnDelimiter . Length * 2 ) ) ;
250+
251+ foreach ( var package in packages )
252+ {
253+ yield return string . Format (
254+ "{0}{1}{2}{3}{4}" ,
255+ package . Id . ToString ( ) . PadRight ( packageIdColumnWidth ) ,
256+ columnDelimiter ,
257+ package . Version . ToNormalizedString ( ) . PadRight ( versionColumnWidth ) ,
258+ columnDelimiter ,
259+ GetCommandsString ( package ) . PadRight ( commandsColumnWidth ) ) ;
260+ }
261+ }
262+
263+ private static bool PackageHasCommands ( IToolPackage package )
264+ {
265+ try
266+ {
267+ return package . Commands . Count >= 0 ;
268+ }
269+ catch ( Exception ex ) when ( ex is ToolConfigurationException )
270+ {
271+ return false ;
272+ }
273+ }
260274 }
261275}
0 commit comments