1+ using System ;
2+ using System . IO ;
3+ using System . Threading . Tasks ;
4+ using FluentAssertions ;
5+ using Newtonsoft . Json . Linq ;
6+ using NuGet . Common ;
7+ using NuGet . Test . Helpers ;
8+ using Sleet ;
9+ using Xunit ;
10+
11+ namespace SleetLib . Tests
12+ {
13+ public class CreateConfigCommandTests
14+ {
15+ [ Fact ]
16+ public async Task CreateConfigCommand_WithLocalStorageType_CreatesValidConfig ( )
17+ {
18+ using ( var testDir = new TestFolder ( ) )
19+ {
20+ var configPath = Path . Combine ( testDir . Root , "sleet.json" ) ;
21+ var result = await CreateConfigCommand . RunAsync ( FileSystemStorageType . Local , testDir . Root , NullLogger . Instance ) ;
22+
23+ result . Should ( ) . BeTrue ( ) ;
24+ File . Exists ( configPath ) . Should ( ) . BeTrue ( ) ;
25+
26+ var json = JObject . Parse ( File . ReadAllText ( configPath ) ) ;
27+ json [ "username" ] . Value < string > ( ) . Should ( ) . Be ( "" ) ;
28+ json [ "useremail" ] . Value < string > ( ) . Should ( ) . Be ( "" ) ;
29+ json [ "sources" ] . Should ( ) . NotBeNull ( ) ;
30+ json [ "sources" ] . Count ( ) . Should ( ) . Be ( 1 ) ;
31+
32+ var source = json [ "sources" ] [ 0 ] ;
33+ source [ "name" ] . Value < string > ( ) . Should ( ) . Be ( "myLocalFeed" ) ;
34+ source [ "type" ] . Value < string > ( ) . Should ( ) . Be ( "local" ) ;
35+ source [ "path" ] . Value < string > ( ) . Should ( ) . NotBeNullOrEmpty ( ) ;
36+ source [ "baseURI" ] . Value < string > ( ) . Should ( ) . Be ( "https://example.com/feed/" ) ;
37+ }
38+ }
39+
40+ [ Fact ]
41+ public async Task CreateConfigCommand_WithAzureStorageType_CreatesValidConfig ( )
42+ {
43+ using ( var testDir = new TestFolder ( ) )
44+ {
45+ var configPath = Path . Combine ( testDir . Root , "sleet.json" ) ;
46+ var result = await CreateConfigCommand . RunAsync ( FileSystemStorageType . Azure , testDir . Root , NullLogger . Instance ) ;
47+
48+ result . Should ( ) . BeTrue ( ) ;
49+ File . Exists ( configPath ) . Should ( ) . BeTrue ( ) ;
50+
51+ var json = JObject . Parse ( File . ReadAllText ( configPath ) ) ;
52+ var source = json [ "sources" ] [ 0 ] ;
53+ source [ "name" ] . Value < string > ( ) . Should ( ) . Be ( "myAzureFeed" ) ;
54+ source [ "type" ] . Value < string > ( ) . Should ( ) . Be ( "azure" ) ;
55+ source [ "container" ] . Value < string > ( ) . Should ( ) . Be ( "myfeed" ) ;
56+ source [ "connectionString" ] . Value < string > ( ) . Should ( ) . Be ( AzureFileSystem . AzureEmptyConnectionString ) ;
57+ }
58+ }
59+
60+ [ Fact ]
61+ public async Task CreateConfigCommand_WithS3StorageType_CreatesValidConfig ( )
62+ {
63+ using ( var testDir = new TestFolder ( ) )
64+ {
65+ var configPath = Path . Combine ( testDir . Root , "sleet.json" ) ;
66+ var result = await CreateConfigCommand . RunAsync ( FileSystemStorageType . S3 , testDir . Root , NullLogger . Instance ) ;
67+
68+ result . Should ( ) . BeTrue ( ) ;
69+ File . Exists ( configPath ) . Should ( ) . BeTrue ( ) ;
70+
71+ var json = JObject . Parse ( File . ReadAllText ( configPath ) ) ;
72+ var source = json [ "sources" ] [ 0 ] ;
73+ source [ "name" ] . Value < string > ( ) . Should ( ) . Be ( "myAmazonS3Feed" ) ;
74+ source [ "type" ] . Value < string > ( ) . Should ( ) . Be ( "s3" ) ;
75+ source [ "bucketName" ] . Value < string > ( ) . Should ( ) . Be ( "bucketname" ) ;
76+ source [ "region" ] . Value < string > ( ) . Should ( ) . Be ( "us-east-1" ) ;
77+ source [ "profileName" ] . Value < string > ( ) . Should ( ) . Be ( "credentialsFileProfileName" ) ;
78+ }
79+ }
80+
81+ [ Fact ]
82+ public async Task CreateConfigCommand_WithUnspecifiedStorageType_CreatesValidConfig ( )
83+ {
84+ using ( var testDir = new TestFolder ( ) )
85+ {
86+ var configPath = Path . Combine ( testDir . Root , "sleet.json" ) ;
87+ var result = await CreateConfigCommand . RunAsync ( FileSystemStorageType . Unspecified , testDir . Root , NullLogger . Instance ) ;
88+
89+ result . Should ( ) . BeTrue ( ) ;
90+ File . Exists ( configPath ) . Should ( ) . BeTrue ( ) ;
91+
92+ var json = JObject . Parse ( File . ReadAllText ( configPath ) ) ;
93+ var source = json [ "sources" ] [ 0 ] ;
94+ source [ "name" ] . Value < string > ( ) . Should ( ) . Be ( "myFeed" ) ;
95+ source [ "type" ] . Value < string > ( ) . Should ( ) . Be ( "" ) ;
96+ }
97+ }
98+
99+ [ Fact ]
100+ public async Task CreateConfigCommand_WithSpecificFileName_CreatesConfigAtPath ( )
101+ {
102+ using ( var testDir = new TestFolder ( ) )
103+ {
104+ var configPath = Path . Combine ( testDir . Root , "custom-config.json" ) ;
105+ var result = await CreateConfigCommand . RunAsync ( FileSystemStorageType . Local , configPath , NullLogger . Instance ) ;
106+
107+ result . Should ( ) . BeTrue ( ) ;
108+ File . Exists ( configPath ) . Should ( ) . BeTrue ( ) ;
109+ }
110+ }
111+
112+ [ Fact ]
113+ public async Task CreateConfigCommand_WithExistingFile_ReturnsFalse ( )
114+ {
115+ using ( var testDir = new TestFolder ( ) )
116+ {
117+ var configPath = Path . Combine ( testDir . Root , "sleet.json" ) ;
118+ File . WriteAllText ( configPath , "existing content" ) ;
119+
120+ var result = await CreateConfigCommand . RunAsync ( FileSystemStorageType . Local , testDir . Root , NullLogger . Instance ) ;
121+
122+ result . Should ( ) . BeFalse ( ) ;
123+ }
124+ }
125+
126+ [ Fact ]
127+ public async Task CreateConfigCommand_WithNonExistentDirectory_ReturnsFalse ( )
128+ {
129+ var nonExistentPath = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) . ToString ( ) , "config.json" ) ;
130+
131+ var result = await CreateConfigCommand . RunAsync ( FileSystemStorageType . Local , nonExistentPath , NullLogger . Instance ) ;
132+
133+ result . Should ( ) . BeFalse ( ) ;
134+ }
135+
136+ [ Fact ]
137+ public async Task CreateConfigCommand_WithNullOutput_UsesCurrentDirectory ( )
138+ {
139+ using ( var testDir = new TestFolder ( ) )
140+ {
141+ var originalDir = Directory . GetCurrentDirectory ( ) ;
142+ try
143+ {
144+ Directory . SetCurrentDirectory ( testDir . Root ) ;
145+ var result = await CreateConfigCommand . RunAsync ( FileSystemStorageType . Local , null , NullLogger . Instance ) ;
146+
147+ result . Should ( ) . BeTrue ( ) ;
148+ File . Exists ( Path . Combine ( testDir . Root , "sleet.json" ) ) . Should ( ) . BeTrue ( ) ;
149+ }
150+ finally
151+ {
152+ Directory . SetCurrentDirectory ( originalDir ) ;
153+ }
154+ }
155+ }
156+
157+ [ Fact ]
158+ public async Task CreateConfigCommand_WithEmptyOutput_UsesCurrentDirectory ( )
159+ {
160+ using ( var testDir = new TestFolder ( ) )
161+ {
162+ var originalDir = Directory . GetCurrentDirectory ( ) ;
163+ try
164+ {
165+ Directory . SetCurrentDirectory ( testDir . Root ) ;
166+ var result = await CreateConfigCommand . RunAsync ( FileSystemStorageType . Local , string . Empty , NullLogger . Instance ) ;
167+
168+ result . Should ( ) . BeTrue ( ) ;
169+ File . Exists ( Path . Combine ( testDir . Root , "sleet.json" ) ) . Should ( ) . BeTrue ( ) ;
170+ }
171+ finally
172+ {
173+ Directory . SetCurrentDirectory ( originalDir ) ;
174+ }
175+ }
176+ }
177+ }
178+ }
0 commit comments