Skip to content

Commit

Permalink
add explicit level to log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
brettfo committed Dec 3, 2024
1 parent 4e35628 commit 50480ff
Show file tree
Hide file tree
Showing 26 changed files with 128 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private class CapturingTestLogger : ILogger

public IReadOnlyList<string> Messages => _messages;

public void Log(string message)
public void LogRaw(string message)
{
_messages.Add(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace NuGetUpdater.Core.Test;

public class TestLogger : ILogger
{
public void Log(string message)
public void LogRaw(string message)
{
Debug.WriteLine(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ public async Task AllPackageDependenciesCanBeTraversed()
temp.DirectoryPath,
temp.DirectoryPath,
"netstandard2.0",
[new Dependency("Package.A", "1.0.0", DependencyType.Unknown)]
[new Dependency("Package.A", "1.0.0", DependencyType.Unknown)],
new TestLogger()
);
AssertEx.Equal(expectedDependencies, actualDependencies);
}
Expand Down Expand Up @@ -300,7 +301,7 @@ public async Task AllPackageDependencies_DoNotTruncateLongDependencyLists()
new Dependency("Package.2A", "1.0.0", DependencyType.Unknown),
new Dependency("Package.2R", "18.0.0", DependencyType.Unknown),
};
var actualDependencies = await MSBuildHelper.GetAllPackageDependenciesAsync(temp.DirectoryPath, temp.DirectoryPath, "net8.0", packages);
var actualDependencies = await MSBuildHelper.GetAllPackageDependenciesAsync(temp.DirectoryPath, temp.DirectoryPath, "net8.0", packages, new TestLogger());
for (int i = 0; i < actualDependencies.Length; i++)
{
var ad = actualDependencies[i];
Expand Down Expand Up @@ -334,7 +335,7 @@ public async Task AllPackageDependencies_DoNotIncludeUpdateOnlyPackages()
new Dependency("Package.B", "2.0.0", DependencyType.Unknown, TargetFrameworks: ["net8.0"]),
new Dependency("Package.C", "3.0.0", DependencyType.Unknown, IsUpdate: true)
};
var actualDependencies = await MSBuildHelper.GetAllPackageDependenciesAsync(temp.DirectoryPath, temp.DirectoryPath, "net8.0", packages);
var actualDependencies = await MSBuildHelper.GetAllPackageDependenciesAsync(temp.DirectoryPath, temp.DirectoryPath, "net8.0", packages, new TestLogger());
AssertEx.Equal(expectedDependencies, actualDependencies);
}

Expand Down Expand Up @@ -371,7 +372,8 @@ await File.WriteAllTextAsync(
temp.DirectoryPath,
temp.DirectoryPath,
"net8.0",
[new Dependency("Some.Package", "4.5.11", DependencyType.Unknown)]
[new Dependency("Some.Package", "4.5.11", DependencyType.Unknown)],
new TestLogger()
);
}
finally
Expand Down Expand Up @@ -428,7 +430,8 @@ await File.WriteAllTextAsync(Path.Join(temp.DirectoryPath, "NuGet.Config"), """
temp.DirectoryPath,
temp.DirectoryPath,
"net8.0",
[new Dependency("Package.A", "1.0.0", DependencyType.Unknown)]
[new Dependency("Package.A", "1.0.0", DependencyType.Unknown)],
new TestLogger()
);

AssertEx.Equal(expectedDependencies, actualDependencies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task<AnalysisResult> RunAsync(string repoRoot, WorkspaceDiscoveryRe
{
var startingDirectory = PathHelper.JoinPath(repoRoot, discovery.Path);

_logger.Log($"Starting analysis of {dependencyInfo.Name}...");
_logger.Info($"Starting analysis of {dependencyInfo.Name}...");

// We need to find all projects which have the given dependency. Even in cases that they
// have it transitively may require that peer dependencies be updated in the project.
Expand Down Expand Up @@ -97,7 +97,7 @@ public async Task<AnalysisResult> RunAsync(string repoRoot, WorkspaceDiscoveryRe
AnalysisResult analysisResult;
if (isUpdateNecessary)
{
_logger.Log($" Determining multi-dependency property.");
_logger.Info($" Determining multi-dependency property.");
var multiDependencies = DetermineMultiDependencyDetails(
discovery,
dependencyInfo.Name,
Expand All @@ -117,7 +117,7 @@ public async Task<AnalysisResult> RunAsync(string repoRoot, WorkspaceDiscoveryRe
.ToImmutableArray()
: projectFrameworks;

_logger.Log($" Finding updated version.");
_logger.Info($" Finding updated version.");
updatedVersion = await FindUpdatedVersionAsync(
startingDirectory,
dependencyInfo,
Expand All @@ -127,7 +127,7 @@ public async Task<AnalysisResult> RunAsync(string repoRoot, WorkspaceDiscoveryRe
_logger,
CancellationToken.None);

_logger.Log($" Finding updated peer dependencies.");
_logger.Info($" Finding updated peer dependencies.");
if (updatedVersion is null)
{
updatedDependencies = [];
Expand Down Expand Up @@ -173,7 +173,7 @@ public async Task<AnalysisResult> RunAsync(string repoRoot, WorkspaceDiscoveryRe
UpdatedDependencies = updatedDependencies,
};

_logger.Log($"Analysis complete.");
_logger.Info($"Analysis complete.");
return analysisResult;
}

Expand Down Expand Up @@ -468,7 +468,7 @@ internal static async Task WriteResultsAsync(string analysisDirectory, string de

var resultPath = Path.Combine(analysisDirectory, $"{dependencyName}.json");

logger.Log($" Writing analysis result to [{resultPath}].");
logger.Info($" Writing analysis result to [{resultPath}].");

var resultJson = JsonSerializer.Serialize(result, SerializerOptions);
await File.WriteAllTextAsync(path: resultPath, resultJson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal static bool PerformCheck(
var incompatibleFrameworks = projectFrameworks.Where(f => !compatibleFrameworks.Contains(f)).ToArray();
if (incompatibleFrameworks.Length > 0)
{
logger.Log($"The package {package} is not compatible. Incompatible project frameworks: {string.Join(", ", incompatibleFrameworks.Select(f => f.GetShortFolderName()))}");
logger.Info($"The package {package} is not compatible. Incompatible project frameworks: {string.Join(", ", incompatibleFrameworks.Select(f => f.GetShortFolderName()))}");
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static async Task<VersionResult> GetVersionsAsync(
var feed = await sourceRepository.GetResourceAsync<MetadataResource>();
if (feed is null)
{
logger.Log($"Failed to get MetadataResource for [{source.Source}]");
logger.Warn($"Failed to get MetadataResource for [{source.Source}]");
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public ShellGitCommandHandler(ILogger logger)

public async Task RunGitCommandAsync(IReadOnlyCollection<string> args, string? workingDirectory = null)
{
_logger.Log($"Running command: git {string.Join(" ", args)}{(workingDirectory is null ? "" : $" in directory {workingDirectory}")}");
_logger.Info($"Running command: git {string.Join(" ", args)}{(workingDirectory is null ? "" : $" in directory {workingDirectory}")}");
var (exitCode, stdout, stderr) = await ProcessEx.RunAsync("git", args, workingDirectory);
HandleErrorsFromOutput(stdout, stderr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task<WorkspaceDiscoveryResult> RunAsync(string repoRootPath, string

if (Directory.Exists(workspacePath))
{
_logger.Log($"Discovering build files in workspace [{workspacePath}].");
_logger.Info($"Discovering build files in workspace [{workspacePath}].");

dotNetToolsJsonDiscovery = DotNetToolsJsonDiscovery.Discover(repoRootPath, workspacePath, _logger);
globalJsonDiscovery = GlobalJsonDiscovery.Discover(repoRootPath, workspacePath, _logger);
Expand All @@ -97,7 +97,7 @@ public async Task<WorkspaceDiscoveryResult> RunAsync(string repoRootPath, string
}
else
{
_logger.Log($"Workspace path [{workspacePath}] does not exist.");
_logger.Info($"Workspace path [{workspacePath}] does not exist.");
}

result = new WorkspaceDiscoveryResult
Expand All @@ -108,7 +108,7 @@ public async Task<WorkspaceDiscoveryResult> RunAsync(string repoRootPath, string
Projects = projectResults.OrderBy(p => p.FilePath).ToImmutableArray(),
};

_logger.Log("Discovery complete.");
_logger.Info("Discovery complete.");
_processedProjectPaths.Clear();

return result;
Expand All @@ -135,19 +135,19 @@ private async Task<bool> TryRestoreMSBuildSdksAsync(string repoRootPath, string

_restoredMSBuildSdks.AddRange(keys);

_logger.Log($" Restoring MSBuild SDKs: {string.Join(", ", keys)}");
_logger.Info($" Restoring MSBuild SDKs: {string.Join(", ", keys)}");

return await NuGetHelper.DownloadNuGetPackagesAsync(repoRootPath, workspacePath, msbuildSdks, logger);
}

private async Task<ImmutableArray<ProjectDiscoveryResult>> RunForDirectoryAsnyc(string repoRootPath, string workspacePath)
{
_logger.Log($" Discovering projects beneath [{Path.GetRelativePath(repoRootPath, workspacePath)}].");
_logger.Info($" Discovering projects beneath [{Path.GetRelativePath(repoRootPath, workspacePath)}].");
var entryPoints = FindEntryPoints(workspacePath);
var projects = ExpandEntryPointsIntoProjects(entryPoints);
if (projects.IsEmpty)
{
_logger.Log(" No project files found.");
_logger.Info(" No project files found.");
return [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ internal static class DotNetToolsJsonDiscovery
{
if (!MSBuildHelper.TryGetDotNetToolsJsonPath(repoRootPath, workspacePath, out var dotnetToolsJsonPath))
{
logger.Log(" No dotnet-tools.json file found.");
logger.Info(" No dotnet-tools.json file found.");
return null;
}

var dotnetToolsJsonFile = DotNetToolsJsonBuildFile.Open(workspacePath, dotnetToolsJsonPath, logger);

logger.Log($" Discovered [{dotnetToolsJsonFile.RelativePath}] file.");
logger.Info($" Discovered [{dotnetToolsJsonFile.RelativePath}] file.");

var dependencies = BuildFile.GetDependencies(dotnetToolsJsonFile)
.OrderBy(d => d.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ internal static class GlobalJsonDiscovery
{
if (!MSBuildHelper.TryGetGlobalJsonPath(repoRootPath, workspacePath, out var globalJsonPath))
{
logger.Log(" No global.json file found.");
logger.Info(" No global.json file found.");
return null;
}

var globalJsonFile = GlobalJsonBuildFile.Open(workspacePath, globalJsonPath, logger);

logger.Log($" Discovered [{globalJsonFile.RelativePath}] file.");
logger.Info($" Discovered [{globalJsonFile.RelativePath}] file.");

var dependencies = BuildFile.GetDependencies(globalJsonFile)
.OrderBy(d => d.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ internal static class PackagesConfigDiscovery

if (packagesConfigPath is null)
{
logger.Log(" No packages.config file found.");
logger.Info(" No packages.config file found.");
return null;
}

var packagesConfigFile = PackagesConfigBuildFile.Open(workspacePath, packagesConfigPath);

logger.Log($" Discovered [{packagesConfigFile.RelativePath}] file.");
logger.Info($" Discovered [{packagesConfigFile.RelativePath}] file.");

var dependencies = BuildFile.GetDependencies(packagesConfigFile)
.OrderBy(d => d.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static async Task<ImmutableArray<ProjectDiscoveryResult>> DiscoverWithBin
if (exitCode != 0)
{
// log error, but still try to resolve what we can
logger.Log($" Error determining dependencies from `{startingProjectPath}`:\nSTDOUT:\n{stdOut}\nSTDERR:\n{stdErr}");
logger.Warn($" Error determining dependencies from `{startingProjectPath}`:\nSTDOUT:\n{stdOut}\nSTDERR:\n{stdErr}");
}

var buildRoot = BinaryLog.ReadBuild(binLogPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public static async Task<ExperimentsManager> FromJobFileAsync(string jobFilePath
}
catch (JsonException ex)
{
// the following message has been specifically designed to match the format of `Dependabot.logger.info(...)` from Ruby
logger.Log($"{DateTime.UtcNow:yyyy/MM/dd HH:mm:ss} INFO Error deserializing job file: {ex.ToString()}: {jobFileContent}");
logger.Info($"Error deserializing job file: {ex.ToString()}: {jobFileContent}");
return new ExperimentsManager();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private void ResetNode()
{
// We can't police that people have legal JSON files.
// If they don't, we just return null.
logger.Log($"Failed to parse JSON file: {RelativePath}, got {ex}");
logger.Warn($"Failed to parse JSON file: {RelativePath}, got {ex}");
FailedToParse = true;
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public static bool IsCompatible(string[] projectTfms, string[] packageTfms, ILog
var incompatibleFrameworks = projectFrameworks.Where(f => !compatibleFrameworks.Contains(f)).ToArray();
if (incompatibleFrameworks.Length > 0)
{
logger.Log($"The package is not compatible. Incompatible project frameworks: {string.Join(", ", incompatibleFrameworks.Select(f => f.GetShortFolderName()))}");
logger.Warn($"The package is not compatible. Incompatible project frameworks: {string.Join(", ", incompatibleFrameworks.Select(f => f.GetShortFolderName()))}");
return false;
}

logger.Log("The package is compatible.");
logger.Info("The package is compatible.");
return true;

static NuGetFramework ParseFramework(string tfm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ private async Task<RunResult> RunForDirectory(Job job, DirectoryInfo repoContent
{
var discoveryResult = await _discoveryWorker.RunAsync(repoContentsPath.FullName, repoDirectory);

_logger.Log("Discovery JSON content:");
_logger.Log(JsonSerializer.Serialize(discoveryResult, DiscoveryWorker.SerializerOptions));
_logger.Info("Discovery JSON content:");
_logger.Info(JsonSerializer.Serialize(discoveryResult, DiscoveryWorker.SerializerOptions));

// report dependencies
var discoveredUpdatedDependencies = GetUpdatedDependencyListFromDiscovery(discoveryResult, repoContentsPath.FullName);
Expand Down Expand Up @@ -155,7 +155,7 @@ async Task TrackOriginalContentsAsync(string directory, string fileName)
}

// do update
_logger.Log($"Running update in directory {repoDirectory}");
_logger.Info($"Running update in directory {repoDirectory}");
foreach (var project in discoveryResult.Projects)
{
foreach (var dependency in project.Dependencies.Where(d => !d.IsTransitive))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ public static async Task UpdateDependencyAsync(
{
if (!MSBuildHelper.TryGetDotNetToolsJsonPath(repoRootPath, workspacePath, out var dotnetToolsJsonPath))
{
logger.Log(" No dotnet-tools.json file found.");
logger.Info(" No dotnet-tools.json file found.");
return;
}

var dotnetToolsJsonFile = DotNetToolsJsonBuildFile.Open(repoRootPath, dotnetToolsJsonPath, logger);

logger.Log($" Updating [{dotnetToolsJsonFile.RelativePath}] file.");
logger.Info($" Updating [{dotnetToolsJsonFile.RelativePath}] file.");

var containsDependency = dotnetToolsJsonFile.GetDependencies().Any(d => d.Name.Equals(dependencyName, StringComparison.OrdinalIgnoreCase));
if (!containsDependency)
{
logger.Log($" Dependency [{dependencyName}] not found.");
logger.Info($" Dependency [{dependencyName}] not found.");
return;
}

Expand All @@ -39,7 +39,7 @@ public static async Task UpdateDependencyAsync(

if (await dotnetToolsJsonFile.SaveAsync())
{
logger.Log($" Saved [{dotnetToolsJsonFile.RelativePath}].");
logger.Info($" Saved [{dotnetToolsJsonFile.RelativePath}].");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ public static async Task UpdateDependencyAsync(
{
if (!MSBuildHelper.TryGetGlobalJsonPath(repoRootPath, workspacePath, out var globalJsonPath))
{
logger.Log(" No global.json file found.");
logger.Info(" No global.json file found.");
return;
}

var globalJsonFile = GlobalJsonBuildFile.Open(repoRootPath, globalJsonPath, logger);

logger.Log($" Updating [{globalJsonFile.RelativePath}] file.");
logger.Info($" Updating [{globalJsonFile.RelativePath}] file.");

var containsDependency = globalJsonFile.GetDependencies().Any(d => d.Name.Equals(dependencyName, StringComparison.OrdinalIgnoreCase));
if (!containsDependency)
{
logger.Log($" Dependency [{dependencyName}] not found.");
logger.Info($" Dependency [{dependencyName}] not found.");
return;
}

if (globalJsonFile.MSBuildSdks?.TryGetPropertyValue(dependencyName, out var version) != true
|| version?.GetValue<string>() is not string versionString)
{
logger.Log(" Unable to determine dependency version.");
logger.Info(" Unable to determine dependency version.");
return;
}

Expand All @@ -43,7 +43,7 @@ public static async Task UpdateDependencyAsync(

if (await globalJsonFile.SaveAsync())
{
logger.Log($" Saved [{globalJsonFile.RelativePath}].");
logger.Info($" Saved [{globalJsonFile.RelativePath}].");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ await MSBuildHelper.SidelineGlobalJsonAsync(projectDirectory, repoRootPath, asyn
var (exitCode, stdout, stderr) = await ProcessEx.RunAsync("dotnet", ["restore", "--force-evaluate", projectPath], workingDirectory: projectDirectory);
if (exitCode != 0)
{
logger.Log($" Lock file update failed.\nSTDOUT:\n{stdout}\nSTDERR:\n{stderr}");
logger.Error($" Lock file update failed.\nSTDOUT:\n{stdout}\nSTDERR:\n{stderr}");
}
return (exitCode, stdout, stderr);
}, logger, retainMSBuildSdks: true);
Expand Down
Loading

0 comments on commit 50480ff

Please sign in to comment.