Skip to content

Make Json's examples run on every supported platform #40

Description

Context and request

examples/General.ps1 hardcodes POSIX paths throughout — /tmp/config.json, /tmp/user-data.json, /tmp/user-settings.json, /tmp/users-pipeline.json, /tmp/user-export.json, /tmp/service-config.json, /tmp/original-config.json, /tmp/modified-config.json — so every file-based example fails on Windows. The module is tested on a three-OS matrix and published to users on all three platforms.

The file also demonstrates behaviour that is being corrected elsewhere. Example 18 performs an import, modify, export round-trip and prints the result; with the current _SourceFile injection that round-trip writes an extra property into the output file, so the example teaches the defect as if it were intended. Example 9 prints $importedConfig._SourceFile explicitly.

examples/ is where realistic end-to-end scenarios live, so these need to be correct, not merely illustrative.

Acceptance criteria.

  • examples/General.ps1 runs to completion on Linux, macOS, and Windows.
  • No absolute POSIX path is hardcoded anywhere in the file.
  • Every temporary file the script creates is removed when it finishes, including on the error paths.
  • No example depends on _SourceFile or any other behaviour scheduled for removal.
  • The examples reflect realistic user scenarios rather than restating command help.

Technical decisions

Temporary paths use [System.IO.Path]::GetTempPath() combined with Join-Path, which resolves correctly on all three platforms, rather than $env:TEMP — which is unset on Linux and macOS.

The _SourceFile references are removed rather than updated. That property is being removed from Import-Json, so an example that prints it will break; leaving it and fixing it later means shipping a knowingly broken example in the interim.

This depends on the _SourceFile correction landing first, so the round-trip example can be verified against the corrected behaviour rather than written speculatively.

Cleanup moves into a finally block so temporary files do not survive a mid-script failure.

examples/ does not match ImportantFilePatterns, so this does not trigger a module build or release on its own.

Implementation plan

  • Replace hardcoded /tmp paths with Join-Path ([System.IO.Path]::GetTempPath()) '<name>'
  • Remove the _SourceFile references from Example 9 and the round-trip in Example 18
  • Move temporary file cleanup into a finally block
  • Re-verify the round-trip example produces output identical in structure to its input
  • Run the full script on Windows and confirm it completes
  • Run the full script on Linux and macOS and confirm it completes
  • Confirm no temporary files remain afterwards on any platform

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions