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
51 changes: 33 additions & 18 deletions examples/New-NetworkDiagram.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,9 @@ param(

function Get-WizardInventoryPath {
param([Parameter(Mandatory)][string]$OutputPath)

$outputDirectory = [System.IO.Path]::GetDirectoryName($OutputPath)
$outputBaseName = [System.IO.Path]::GetFileNameWithoutExtension($OutputPath)

if ([string]::IsNullOrEmpty($outputBaseName)) {
$outputBaseName = [System.IO.Path]::GetFileName($OutputPath)
}

$inventoryFileName = "$outputBaseName-inventory.json"
if ([string]::IsNullOrEmpty($outputDirectory)) {
return $inventoryFileName
}

return (Join-Path $outputDirectory $inventoryFileName)
return Get-InventoryOutputPath -DiagramPath $OutputPath
}

$inventoryPath = Get-WizardInventoryPath -OutputPath $OutputPath

# ── IPv4 CIDR helpers ────────────────────────────────────────────────────────
# Real prefix-aware host enumeration (replaces the old /24-only "first 3 octets"
# assumption). Works for any prefix; the caller caps how many hosts get scanned.
Expand Down Expand Up @@ -121,7 +106,35 @@ function Get-SubnetScanTarget {
return $hostValues.ToArray()
}

# Allow the Pester suite to dot-source this script for the CIDR helper functions above
function Get-InventoryOutputPath {
param([Parameter(Mandatory)][string]$DiagramPath)

if ([string]::IsNullOrWhiteSpace($DiagramPath)) {
throw 'Diagram output path cannot be empty.'
}

$fileName = [System.IO.Path]::GetFileNameWithoutExtension($DiagramPath)
if ([string]::IsNullOrWhiteSpace($fileName)) {
throw "Diagram output path must include a file name: $DiagramPath"
}

$directory = [System.IO.Path]::GetDirectoryName($DiagramPath)
$inventoryFileName = "$fileName-inventory.json"
$inventoryPath = if ([string]::IsNullOrEmpty($directory)) {
$inventoryFileName
}
else {
[System.IO.Path]::Combine($directory, $inventoryFileName)
}

if ([System.IO.Path]::GetFullPath($inventoryPath) -eq [System.IO.Path]::GetFullPath($DiagramPath)) {
throw 'Diagram and inventory output paths must be different.'
}

return $inventoryPath
}

# Allow the Pester suite to dot-source this script for the pure helper functions above
# without launching the interactive wizard (which performs a live network scan).
if ($MyInvocation.InvocationName -eq '.') { return }

Expand Down Expand Up @@ -503,14 +516,16 @@ $topology = [pscustomobject]@{
# Step 5: Generate diagram
Write-Host "`n[5/5] Generating your network diagram..." -ForegroundColor Yellow

$inventoryPath = Get-InventoryOutputPath -DiagramPath $OutputPath
$topology | Export-DrawIO -OutFile $OutputPath

if (Test-Path $OutputPath) {
$fileSize = (Get-Item $OutputPath).Length
Write-Host " ✓ Created: $OutputPath ($fileSize bytes)" -ForegroundColor Green
}

# Save inventory for future use
# Save inventory for future use. Every accepted diagram extension produces a
# distinct sibling inventory file.
$inventoryData = @{
knownDevices = @($nodes | ForEach-Object {
@{
Expand Down
30 changes: 30 additions & 0 deletions tests/NetDiagram.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,33 @@ Describe 'Quick-start wizard CIDR math (#4 regression)' {
$inventoryPath | Should -Not -Be $outputPath
}
}

Describe 'Quick-start wizard output paths (#9 regression)' {
BeforeAll {
$exampleScript = Join-Path $PSScriptRoot '..' 'examples' 'New-NetworkDiagram.ps1'
. $exampleScript
}

It 'Derives a sibling inventory path for a drawio diagram' {
Get-InventoryOutputPath -DiagramPath './office.drawio' |
Should -Be (Join-Path '.' 'office-inventory.json')
}

It 'Keeps an XML diagram and its inventory distinct' {
$diagramPath = Join-Path $TestDrive 'office.xml'
$inventoryPath = Get-InventoryOutputPath -DiagramPath $diagramPath

$inventoryPath | Should -Be (Join-Path $TestDrive 'office-inventory.json')
[System.IO.Path]::GetFullPath($inventoryPath) |
Should -Not -Be ([System.IO.Path]::GetFullPath($diagramPath))
}

It 'Keeps an extensionless diagram and its inventory distinct' {
$diagramPath = Join-Path $TestDrive 'office'
$inventoryPath = Get-InventoryOutputPath -DiagramPath $diagramPath

$inventoryPath | Should -Be (Join-Path $TestDrive 'office-inventory.json')
[System.IO.Path]::GetFullPath($inventoryPath) |
Should -Not -Be ([System.IO.Path]::GetFullPath($diagramPath))
}
}
Loading