Skip to content

Release Notes v0.5.0

Released: 2026-07-12

Highlights

Cross-platform test utilities for home directory and duration assertions.

New Features

testutil.SetTempHomeDir

Set a temporary directory as the home directory for the duration of a test, handling cross-platform differences automatically.

Problem solved: os.UserHomeDir() reads different environment variables on different platforms:

Platform Environment Variable
Unix/Linux/macOS HOME
Windows USERPROFILE

Tests that only set HOME will fail on Windows when code uses os.UserHomeDir().

Solution:

import "github.com/grokify/oscompat/testutil"

func TestConfigLoading(t *testing.T) {
    tmpDir := testutil.SetTempHomeDir(t)

    // os.UserHomeDir() now returns tmpDir on all platforms
    config, err := LoadConfig() // Uses home directory internally
    // ...
}

The function:

  • Creates a temporary directory via t.TempDir()
  • Sets both HOME and USERPROFILE environment variables
  • Automatically restores original values when test completes (via t.Setenv)
  • Returns the temporary directory path for test use

Duration Assertion Utilities

New utilities for Windows-safe duration assertions in tests.

Problem solved: Windows has a system timer resolution of approximately 15.6ms, which can cause timing-based tests to report slightly negative durations due to clock drift between measurements.

Function Description
DurationNonNegative(d) Returns true if duration >= -WindowsTimerResolution
AssertDurationNonNegative(t, d, msg) Test helper that fails if duration is too negative
WindowsTimerResolution Constant: 16ms tolerance for Windows timer resolution

Example:

import "github.com/grokify/oscompat/testutil"

func TestOperationTiming(t *testing.T) {
    start := time.Now()
    doOperation()
    elapsed := time.Since(start)

    // Windows-safe: allows small negative values due to timer resolution
    testutil.AssertDurationNonNegative(t, elapsed, "operation duration")
}

Installation

go get github.com/grokify/oscompat@v0.5.0