Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions IISParser.PowerShell/AsyncPSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,23 @@ protected ActionPreference GetErrorActionPreference() {
/// </summary>
/// <param name="path">The file path to check.</param>
/// <param name="errorAction">The action preference determining error handling.</param>
/// <param name="resolvedPath">The resolved provider path.</param>
/// <returns><c>true</c> if the file exists; otherwise, <c>false</c>.</returns>
protected bool EnsureFileExists(string path, ActionPreference errorAction) {
if (File.Exists(path)) {
protected bool EnsureFileExists(string path, ActionPreference errorAction, out string resolvedPath) {
try {
resolvedPath = GetUnresolvedProviderPathFromPSPath(path);
} catch (ItemNotFoundException) {
resolvedPath = path;
}

if (File.Exists(resolvedPath)) {
return true;
}

string message = $"{MyInvocation.InvocationName} - The specified file does not exist: {path}";
string message = $"{MyInvocation.InvocationName} - The specified file does not exist: {resolvedPath}";
if (errorAction == ActionPreference.Stop) {
FileNotFoundException ex = new("The specified file does not exist.", path);
ThrowTerminatingError(new ErrorRecord(ex, "FileNotFound", ErrorCategory.ObjectNotFound, path));
FileNotFoundException ex = new("The specified file does not exist.", resolvedPath);
ThrowTerminatingError(new ErrorRecord(ex, "FileNotFound", ErrorCategory.ObjectNotFound, resolvedPath));
} else {
LoggingMessages.Logger.WriteWarning(message);
}
Expand Down
14 changes: 7 additions & 7 deletions IISParser.PowerShell/CmdletGetIISParsedLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ public class CmdletGetIISParsedLog : AsyncPSCmdlet {
public SwitchParameter Legacy { get; set; }

/// <inheritdoc />
protected override Task BeginProcessingAsync() {
ActionPreference pref = GetErrorActionPreference();
if (EnsureFileExists(FilePath, pref)) {
_parser = new ParserEngine(FilePath);
}
return Task.CompletedTask;
}
protected override Task BeginProcessingAsync() {
ActionPreference pref = GetErrorActionPreference();
if (EnsureFileExists(FilePath, pref, out string resolvedPath)) {
_parser = new ParserEngine(resolvedPath);
}
return Task.CompletedTask;
}

/// <inheritdoc />
protected override Task ProcessRecordAsync() {
Expand Down
14 changes: 14 additions & 0 deletions IISParser.Tests/ParserEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ public void ParseLog_ReturnsRecords() {
Assert.Equal("192.168.0.1", record.Fields["X-Forwarded-For"]);
}

[Fact]
public void ParseLog_SupportsRelativePath() {
var baseDir = Path.Combine(AppContext.BaseDirectory, "TestData");
var previous = Environment.CurrentDirectory;
try {
Environment.CurrentDirectory = baseDir;
var engine = new ParserEngine("./sample.log");
var record = engine.ParseLog().Single();
Assert.Equal("/index.html", record.UriPath);
} finally {
Environment.CurrentDirectory = previous;
}
}

[Fact]
public void ParseLog_RemovesKnownFieldsFromDictionary() {
var path = Path.Combine(AppContext.BaseDirectory, "TestData", "sample.log");
Expand Down
6 changes: 6 additions & 0 deletions Module/Examples/GetLogsRelative.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Import-Module $PSScriptRoot\..\IISParser.psd1 -Force

$logDir = Join-Path $PSScriptRoot '..\..\IISParser.Tests\TestData'
Push-Location $logDir
Get-IISParsedLog -FilePath '.\sample.log' -First 1 | Format-Table
Pop-Location
11 changes: 11 additions & 0 deletions Module/Tests/Get-IISParsedLog.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ Describe 'Get-IISParsedLog' {
($result.PSObject.Properties.Match('Fields').Count) | Should -Be 0
}

It 'accepts relative file paths' {
$logPath = (Resolve-Path "$PSScriptRoot/../../IISParser.Tests/TestData/sample.log").Path
Push-Location (Split-Path $logPath)
try {
$result = Get-IISParsedLog -FilePath './sample.log'
$result.UriPath | Should -Be '/index.html'
} finally {
Pop-Location
}
}

It 'streams large log with Skip, First and Last' {
$logPath = Join-Path $TestDrive 'large.log'
$header = '#Fields: date time s-ip cs-method cs-uri-stem sc-status X-Forwarded-For'
Expand Down
Loading