Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit cd984f5

Browse files
committed
Create the Dotnet User Profile folder when running the first experience, if the folder does not exist, if will fail the first run because it will fail to create the first notice sentinel.
1 parent f520b8a commit cd984f5

File tree

2 files changed

+187
-2
lines changed

2 files changed

+187
-2
lines changed

src/Microsoft.DotNet.Configurer/FirstTimeUseNoticeSentinel.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,24 @@ public class FirstTimeUseNoticeSentinel : IFirstTimeUseNoticeSentinel
1313
public static readonly string SENTINEL = $"{Product.Version}.dotnetFirstUseSentinel";
1414

1515
private readonly IFile _file;
16+
private readonly IDirectory _directory;
1617

1718
private string _dotnetUserProfileFolderPath;
1819

1920
private string SentinelPath => Path.Combine(_dotnetUserProfileFolderPath, SENTINEL);
2021

2122
public FirstTimeUseNoticeSentinel(CliFallbackFolderPathCalculator cliFallbackFolderPathCalculator) :
22-
this(cliFallbackFolderPathCalculator.DotnetUserProfileFolderPath, FileSystemWrapper.Default.File)
23+
this(
24+
cliFallbackFolderPathCalculator.DotnetUserProfileFolderPath,
25+
FileSystemWrapper.Default.File,
26+
FileSystemWrapper.Default.Directory)
2327
{
2428
}
2529

26-
internal FirstTimeUseNoticeSentinel(string dotnetUserProfileFolderPath, IFile file)
30+
internal FirstTimeUseNoticeSentinel(string dotnetUserProfileFolderPath, IFile file, IDirectory directory)
2731
{
2832
_file = file;
33+
_directory = directory;
2934
_dotnetUserProfileFolderPath = dotnetUserProfileFolderPath;
3035
}
3136

@@ -38,6 +43,11 @@ public void CreateIfNotExists()
3843
{
3944
if (!Exists())
4045
{
46+
if (!_directory.Exists(_dotnetUserProfileFolderPath))
47+
{
48+
_directory.CreateDirectory(_dotnetUserProfileFolderPath);
49+
}
50+
4151
_file.CreateEmptyFile(SentinelPath);
4252
}
4353
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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

Comments
 (0)