Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
matzefriedrich committed Apr 17, 2024
1 parent e3523a6 commit 38595f8
Show file tree
Hide file tree
Showing 24 changed files with 136 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ public void CurrentDirectoryWorkspaceEnvironment_WorkspaceConfigurationFilePath_
{
// Arrange
var sut = new CurrentDirectoryWorkspaceEnvironment();

// Act
string actual = sut.WorkspaceConfigurationFilePath();

// Assert
Assert.False(string.IsNullOrWhiteSpace(actual));
Assert.StartsWith(Environment.CurrentDirectory, actual);
}

}
}
4 changes: 2 additions & 2 deletions src/dotnet.nugit.UnitTest/LocalNuGetFeedServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task LocalNuGetFeedService_CreateLocalFeedIfNotExistsAsync_Test()
"</configuration>";

var buffer = new StringBuilder(nugetConfig);

var variablesServiceMock = new Mock<IVariablesService>();
variablesServiceMock
.Setup(service => service.TryGetVariable(ApplicationVariableNames.NugitHome, out nugitHomeDirectoryVariableValue))
Expand All @@ -68,7 +68,7 @@ public async Task LocalNuGetFeedService_CreateLocalFeedIfNotExistsAsync_Test()
// Act
LocalFeedInfo? createdFeed = await sut.CreateLocalFeedIfNotExistsAsync(CancellationToken.None);
LocalFeedInfo? actual = await sut.GetConfiguredLocalFeedAsync(CancellationToken.None);

// Assert
Assert.NotNull(createdFeed);
Assert.Equal("LocalNuGitFeed", createdFeed.Name);
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet.nugit.UnitTest/NugitHomeVariableAccessorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public void NugitHomeVariableAccessor_Value_Test()
{
// Arrange
var sut = new NugitHomeVariableAccessor();

// Act
string actual = sut.Value();

// Assert
Assert.Equal(ApplicationVariableNames.NugitHome, sut.Name);
Assert.False(string.IsNullOrWhiteSpace(actual));
Expand Down
16 changes: 8 additions & 8 deletions src/dotnet.nugit.UnitTest/dotnet.nugit.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0"/>
<PackageReference Include="Moq" Version="4.20.70"/>
<PackageReference Include="xunit" Version="2.7.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

Expand All @@ -28,7 +28,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\dotnet.nugit\dotnet.nugit.csproj" />
<ProjectReference Include="..\dotnet.nugit\dotnet.nugit.csproj"/>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/dotnet.nugit/Abstractions/ApplicationVariableNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static string GetNugitHomeDirectoryPath(this IVariablesService variablesS
ArgumentNullException.ThrowIfNull(variablesService);
if (variablesService.TryGetVariable(NugitHome, out string? value) == false || string.IsNullOrWhiteSpace(value))
throw new InvalidOperationException($"The {NugitHome} variable is not set.");

return value;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet.nugit/Abstractions/IDotNetUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public interface IDotNetUtility
{
Task BuildAsync(string projectFile, TimeSpan? timeout = null, CancellationToken cancellationToken = default);

Task<bool> TryPackAsync(string projectFile, string targetFolderPath, PackOptions options, TimeSpan? timeout = null, CancellationToken cancellationToken = default);
}
}
2 changes: 0 additions & 2 deletions src/dotnet.nugit/Abstractions/IFindFilesService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace dotnet.nugit.Abstractions
{
using Services;

public interface IFindFilesService
{
IAsyncEnumerable<string> FindAsync(string path, string pattern, Func<FileSystemEntry, Task<bool>> fetch, CancellationToken cancellationToken);
Expand Down
2 changes: 0 additions & 2 deletions src/dotnet.nugit/Abstractions/ILibGit2SharpAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace dotnet.nugit.Abstractions
{
using LibGit2Sharp;

public interface ILibGit2SharpAdapter
{
bool TryCloneRepository(string cloneUrl, string repositoryPath, CancellationToken cancellationToken);
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet.nugit/Abstractions/INuGetFeedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public interface INuGetFeedService
{
Task<LocalFeedInfo?> GetConfiguredLocalFeedAsync(CancellationToken cancellationToken);

Task<IEnumerable<PackageSource>> GetConfiguredPackageSourcesAsync(CancellationToken cancellationToken);

Task<LocalFeedInfo?> CreateLocalFeedIfNotExistsAsync(CancellationToken cancellationToken);
Expand Down
20 changes: 9 additions & 11 deletions src/dotnet.nugit/Abstractions/LocalFeedInfo.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
namespace dotnet.nugit.Abstractions
{
using Resources;

public class LocalFeedInfo : IEquatable<LocalFeedInfo>
{
// ReSharper disable once EmptyConstructor; the empty ctor is required by the Yaml deserializer
public LocalFeedInfo()
{
}

public required string Name { get; init; }
public required string LocalPath { get; init; }

public bool Equals(LocalFeedInfo? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return this.Name == other.Name && this.LocalPath == other.LocalPath;
}

public string PackagesPath()
{
return Path.Combine(this.LocalPath, "packages");
Expand All @@ -22,19 +27,12 @@ public string RepositoriesPath()
return Path.Combine(this.LocalPath, "repositories");
}

public bool Equals(LocalFeedInfo? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return this.Name == other.Name && this.LocalPath == other.LocalPath;
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((LocalFeedInfo)obj);
return this.Equals((LocalFeedInfo)obj);
}

public override int GetHashCode()
Expand Down
12 changes: 3 additions & 9 deletions src/dotnet.nugit/Abstractions/PackageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@ public sealed class PackageSource(string key, string? value) : IEquatable<Packag

public bool Equals(PackageSource? other)
{
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(null, other)) return false;

if (ReferenceEquals(this, other))
{
return true;
}
if (ReferenceEquals(this, other)) return true;

return this.Key == other.Key && this.Value == other.Value && this.ProtocolVersion == other.ProtocolVersion;
}

public override bool Equals(object? obj)
{
return ReferenceEquals(this, obj) || obj is PackageSource other && Equals(other);
return ReferenceEquals(this, obj) || (obj is PackageSource other && this.Equals(other));
}

public override int GetHashCode()
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet.nugit/Abstractions/RepositoryReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override bool Equals(object? obj)
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((RepositoryReference)obj);
return this.Equals((RepositoryReference)obj);
}

public override int GetHashCode()
Expand Down
6 changes: 3 additions & 3 deletions src/dotnet.nugit/Abstractions/RepositoryUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ string FormatUriString(string uriString)
{
var builder = new StringBuilder(uriString);

if (string.IsNullOrWhiteSpace(this.Tag) == false)
if (string.IsNullOrWhiteSpace(this.Tag) == false)
builder.Append($"@{this.Tag}");
if (string.IsNullOrWhiteSpace(this.Path) == false)

if (string.IsNullOrWhiteSpace(this.Path) == false)
builder.Append($"/{this.Path}");

return builder.ToString();
Expand Down
5 changes: 1 addition & 4 deletions src/dotnet.nugit/Abstractions/VariableAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ public abstract class VariableAccessor
{
protected VariableAccessor(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(Resources.ArgumentException_Value_cannot_be_null_or_whitespace, nameof(name));
}
if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException(Resources.ArgumentException_Value_cannot_be_null_or_whitespace, nameof(name));

this.Name = name;
}
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet.nugit/Commands/InitCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public async Task<int> InitializeNewRepositoryAsync(bool copyLocal)
if (feed == null) return ErrCannotCreateFeed;

this.logger.LogDebug("Feed: {0}, Path: {1}", feed.Name, feed.LocalPath);

return Ok;
}

private async Task CreateNugitRepositoryFileIfNotExistsAsync(LocalFeedInfo? feed, bool copyLocal = false)
{
await this.workspace.CreateOrUpdateConfigurationAsync(CreateNewConfigurationFile,UpdateConfigurationFile);
await this.workspace.CreateOrUpdateConfigurationAsync(CreateNewConfigurationFile, UpdateConfigurationFile);

return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class ListEnvironmentVariablesCommand(
IVariablesService variablesService,
ILogger<ListEnvironmentVariablesCommand> logger)
{
private readonly IVariablesService variablesService = variablesService ?? throw new ArgumentNullException(nameof(variablesService));
private readonly ILogger<ListEnvironmentVariablesCommand> logger = logger ?? throw new ArgumentNullException(nameof(logger));
private readonly IVariablesService variablesService = variablesService ?? throw new ArgumentNullException(nameof(variablesService));

public async Task<int> ListEnvironmentVariablesAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ namespace dotnet.nugit.Services
{
public sealed class CurrentDirectoryWorkspaceEnvironment : DirectoryWorkspaceEnvironment
{
protected override string DirectoryPath() => Environment.CurrentDirectory;
protected override string DirectoryPath()
{
return Environment.CurrentDirectory;
}
}
}
4 changes: 2 additions & 2 deletions src/dotnet.nugit/Services/DirectoryWorkspaceEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace dotnet.nugit.Services
public abstract class DirectoryWorkspaceEnvironment : IWorkspaceEnvironment
{
private const string WorkspaceConfigurationFileName = ".nugit";

private static readonly Encoding ConfigurationFileEncoding = Encoding.UTF8;

public TextReader CreateConfigurationFileReader()
{
string nugitFile = this.WorkspaceConfigurationFilePath();
Expand Down
6 changes: 3 additions & 3 deletions src/dotnet.nugit/Services/LibGit2SharpAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public LibGit2SharpAdapter(ILogger<LibGit2SharpAdapter> logger)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public bool TryCloneRepository(string cloneUrl, string repositoryPath, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(cloneUrl)) throw new ArgumentException(Resources.ArgumentException_Value_cannot_be_null_or_whitespace, nameof(cloneUrl));
if (string.IsNullOrWhiteSpace(repositoryPath)) throw new ArgumentException(Resources.ArgumentException_Value_cannot_be_null_or_whitespace, nameof(repositoryPath));

this.resetEventSlim.Reset();

var cloneOptions = new CloneOptions
{
RecurseSubmodules = true,
Expand All @@ -48,7 +48,7 @@ public bool TryCloneRepository(string cloneUrl, string repositoryPath, Cancellat

return false;
}

private void RepositoryOperationCompleted(RepositoryOperationContext context)
{
this.logger.LogInformation("Completed operating on the repository.");
Expand Down
6 changes: 3 additions & 3 deletions src/dotnet.nugit/Services/LocalNuGetFeedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public sealed class LocalNuGetFeedService(
public async Task<IEnumerable<PackageSource>> GetConfiguredPackageSourcesAsync(CancellationToken cancellationToken)
{
using TextReader reader = this.infoService.GetNuGetConfigReader();
if (reader == StreamReader.Null)
if (reader == StreamReader.Null)
return Array.Empty<PackageSource>();

XDocument doc = await XDocument.LoadAsync(reader, LoadOptions.None, cancellationToken);

XElement? configurationElt = doc.Element("configuration");
Expand Down Expand Up @@ -65,7 +65,7 @@ public async Task<IEnumerable<PackageSource>> GetConfiguredPackageSourcesAsync(C
if (reader == StreamReader.Null) return null;
doc = await XDocument.LoadAsync(reader, LoadOptions.None, cancellationToken);
}

XElement? configurationElt = doc.Element("configuration");
XElement? sourcesElt = configurationElt?.Element("packageSources");
sourcesElt?.Add(new XElement("add", new XAttribute("key", feedName), new XAttribute("value", localFeedPath)));
Expand Down
11 changes: 5 additions & 6 deletions src/dotnet.nugit/Services/TempDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@
{
public sealed class TempDirectory : IDisposable
{
private bool disposed;
private readonly object syncObject = new();
private readonly string path;
private bool disposed;

public TempDirectory()
{
string folderName = Guid.NewGuid().ToString()[..8];
this.path = Path.Combine(Path.GetTempPath(), folderName);
this.DirectoryPath = Path.Combine(Path.GetTempPath(), folderName);

lock (this.syncObject)
{
if (Directory.Exists(this.path) == false) Directory.CreateDirectory(this.path);
if (Directory.Exists(this.DirectoryPath) == false) Directory.CreateDirectory(this.DirectoryPath);
}
}

public string DirectoryPath => this.path;
public string DirectoryPath { get; }

public void Dispose()
{
lock (this.syncObject)
{
if (this.disposed) return;
if (Directory.Exists(this.path)) Directory.Delete(this.path, true);
if (Directory.Exists(this.DirectoryPath)) Directory.Delete(this.DirectoryPath, true);
this.disposed = true;
}
}
Expand Down
10 changes: 2 additions & 8 deletions src/dotnet.nugit/Services/VariablesService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ public IEnumerable<string> GetVariableNames()

public bool TryGetVariable(string name, out string? value)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException(Resources.ArgumentException_Value_cannot_be_null_or_whitespace, nameof(name));
}
if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException(Resources.ArgumentException_Value_cannot_be_null_or_whitespace, nameof(name));

string lookupName = name.ToLowerInvariant();
IDictionary<string, string> variables = this.GetCurrentProcessVariables();
Expand All @@ -38,10 +35,7 @@ private IDictionary<string, string> GetCurrentProcessVariables()
string variableLookupName = variableName.ToLowerInvariant();
processVariables[variableLookupName] = variable.Value() ?? string.Empty;
string? value = Environment.GetEnvironmentVariable(variableName, EnvironmentVariableTarget.Process)?.Trim();
if (string.IsNullOrWhiteSpace(value) == false)
{
processVariables[variableLookupName] = value;
}
if (string.IsNullOrWhiteSpace(value) == false) processVariables[variableLookupName] = value;
}

return processVariables;
Expand Down
Loading

0 comments on commit 38595f8

Please sign in to comment.