1+ // Copyright (c) .NET Foundation and contributors. All rights reserved.
2+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+ using System ;
5+ using System . Collections . Generic ;
6+ using System . IO ;
7+ using System . Linq ;
8+ using FluentAssertions ;
9+ using Microsoft . DotNet . Cli . Utils ;
10+ using Microsoft . DotNet . Configurer ;
11+ using Microsoft . Extensions . DependencyModel . Tests ;
12+ using Microsoft . Extensions . EnvironmentAbstractions ;
13+ using Moq ;
14+ using Xunit ;
15+
16+ namespace Microsoft . DotNet . Configurer . UnitTests
17+ {
18+ public class GivenAFirstTimeUseNoticeSentinel
19+ {
20+ private const string DOTNET_USER_PROFILE_FOLDER_PATH = "some path" ;
21+
22+ private FileSystemMockBuilder _fileSystemMockBuilder ;
23+
24+ public GivenAFirstTimeUseNoticeSentinel ( )
25+ {
26+ _fileSystemMockBuilder = FileSystemMockBuilder . Create ( ) ;
27+ }
28+
29+ [ Fact ]
30+ public void TheSentinelHasTheCurrentVersionInItsName ( )
31+ {
32+ FirstTimeUseNoticeSentinel . SENTINEL . Should ( ) . Contain ( $ "{ Product . Version } ") ;
33+ }
34+
35+ [ Fact ]
36+ public void ItReturnsTrueIfTheSentinelExists ( )
37+ {
38+ _fileSystemMockBuilder . AddFiles ( DOTNET_USER_PROFILE_FOLDER_PATH , FirstTimeUseNoticeSentinel . SENTINEL ) ;
39+
40+ var fileSystemMock = _fileSystemMockBuilder . Build ( ) ;
41+
42+ var firstTimeUseNoticeSentinel =
43+ new FirstTimeUseNoticeSentinel (
44+ DOTNET_USER_PROFILE_FOLDER_PATH ,
45+ fileSystemMock . File ,
46+ fileSystemMock . Directory ) ;
47+
48+ firstTimeUseNoticeSentinel . Exists ( ) . Should ( ) . BeTrue ( ) ;
49+ }
50+
51+ [ Fact ]
52+ public void ItReturnsFalseIfTheSentinelDoesNotExist ( )
53+ {
54+ var fileSystemMock = _fileSystemMockBuilder . Build ( ) ;
55+
56+ var firstTimeUseNoticeSentinel =
57+ new FirstTimeUseNoticeSentinel (
58+ DOTNET_USER_PROFILE_FOLDER_PATH ,
59+ fileSystemMock . File ,
60+ fileSystemMock . Directory ) ;
61+
62+ firstTimeUseNoticeSentinel . Exists ( ) . Should ( ) . BeFalse ( ) ;
63+ }
64+
65+ [ Fact ]
66+ public void ItCreatesTheSentinelInTheDotnetUserProfileFolderPathIfItDoesNotExistAlready ( )
67+ {
68+ var fileSystemMock = _fileSystemMockBuilder . Build ( ) ;
69+ var firstTimeUseNoticeSentinel =
70+ new FirstTimeUseNoticeSentinel (
71+ DOTNET_USER_PROFILE_FOLDER_PATH ,
72+ fileSystemMock . File ,
73+ fileSystemMock . Directory ) ;
74+
75+ firstTimeUseNoticeSentinel . Exists ( ) . Should ( ) . BeFalse ( ) ;
76+
77+ firstTimeUseNoticeSentinel . CreateIfNotExists ( ) ;
78+
79+ firstTimeUseNoticeSentinel . Exists ( ) . Should ( ) . BeTrue ( ) ;
80+ }
81+
82+ [ Fact ]
83+ public void ItDoesNotCreateTheSentinelAgainIfItAlreadyExistsInTheDotnetUserProfileFolderPath ( )
84+ {
85+ const string contentToValidateSentinalWasNotReplaced = "some string" ;
86+ var sentinel = Path . Combine ( DOTNET_USER_PROFILE_FOLDER_PATH , FirstTimeUseNoticeSentinel . SENTINEL ) ;
87+ _fileSystemMockBuilder . AddFile ( sentinel , contentToValidateSentinalWasNotReplaced ) ;
88+
89+ var fileSystemMock = _fileSystemMockBuilder . Build ( ) ;
90+
91+ var firstTimeUseNoticeSentinel =
92+ new FirstTimeUseNoticeSentinel (
93+ DOTNET_USER_PROFILE_FOLDER_PATH ,
94+ fileSystemMock . File ,
95+ fileSystemMock . Directory ) ;
96+
97+ firstTimeUseNoticeSentinel . Exists ( ) . Should ( ) . BeTrue ( ) ;
98+
99+ firstTimeUseNoticeSentinel . CreateIfNotExists ( ) ;
100+
101+ fileSystemMock . File . ReadAllText ( sentinel ) . Should ( ) . Be ( contentToValidateSentinalWasNotReplaced ) ;
102+ }
103+
104+ [ Fact ]
105+ public void ItCreatesTheDotnetUserProfileFolderIfItDoesNotExistAlreadyWhenCreatingTheSentinel ( )
106+ {
107+ var fileSystemMock = _fileSystemMockBuilder . Build ( ) ;
108+ var directoryMock = new DirectoryMock ( ) ;
109+ var firstTimeUseNoticeSentinel =
110+ new FirstTimeUseNoticeSentinel (
111+ DOTNET_USER_PROFILE_FOLDER_PATH ,
112+ fileSystemMock . File ,
113+ directoryMock ) ;
114+
115+ firstTimeUseNoticeSentinel . CreateIfNotExists ( ) ;
116+
117+ directoryMock . Exists ( DOTNET_USER_PROFILE_FOLDER_PATH ) . Should ( ) . BeTrue ( ) ;
118+ directoryMock . CreateDirectoryInvoked . Should ( ) . BeTrue ( ) ;
119+ }
120+
121+ [ Fact ]
122+ public void ItDoesNotAttemptToCreateTheDotnetUserProfileFolderIfItAlreadyExistsWhenCreatingTheSentinel ( )
123+ {
124+ var fileSystemMock = _fileSystemMockBuilder . Build ( ) ;
125+ var directoryMock = new DirectoryMock ( new List < string > { DOTNET_USER_PROFILE_FOLDER_PATH } ) ;
126+ var firstTimeUseNoticeSentinel =
127+ new FirstTimeUseNoticeSentinel (
128+ DOTNET_USER_PROFILE_FOLDER_PATH ,
129+ fileSystemMock . File ,
130+ directoryMock ) ;
131+
132+ firstTimeUseNoticeSentinel . CreateIfNotExists ( ) ;
133+
134+ directoryMock . CreateDirectoryInvoked . Should ( ) . BeFalse ( ) ;
135+ }
136+
137+ private class DirectoryMock : IDirectory
138+ {
139+ private IList < string > _directories ;
140+
141+ public bool CreateDirectoryInvoked { get ; set ; }
142+
143+ public DirectoryMock ( IList < string > directories = null )
144+ {
145+ _directories = directories ?? new List < string > ( ) ;
146+ }
147+
148+ public bool Exists ( string path )
149+ {
150+ return _directories . Any ( d => d == path ) ;
151+ }
152+
153+ public ITemporaryDirectory CreateTemporaryDirectory ( )
154+ {
155+ throw new NotImplementedException ( ) ;
156+ }
157+
158+ public IEnumerable < string > GetFiles ( string path , string searchPattern )
159+ {
160+ throw new NotImplementedException ( ) ;
161+ }
162+
163+ public string GetDirectoryFullName ( string path )
164+ {
165+ throw new NotImplementedException ( ) ;
166+ }
167+
168+ public void CreateDirectory ( string path )
169+ {
170+ _directories . Add ( path ) ;
171+ CreateDirectoryInvoked = true ;
172+ }
173+ }
174+ }
175+ }
0 commit comments