tests: consolidate duplicated test setup code and config files

Extract the shared boilerplate (macports init, tmp prefix setup,
mportinit, macports_worker_init) from 27 test files into a single
test_setup.tcl sourced by all unit test suites.  Merge the three
divergent copies of macports_worker_init into one consolidated
superset.  Fixes #73828.

Eliminate the duplicate sources.conf files in port1.0/tests/ and
package1.0/tests/ by having init_tmp_prefix derive the path from
top_srcdir, leaving a single copy in macports1.0/tests/.  Fixes
#73827.

Expose the test tclsh path as macports::autoconf::test_tclsh so
tests can reference it without recomputing the path from
top_srcdir.

Replace the hardcoded /tmp/macports-tests path in the integration
test infrastructure with a unique temporary directory created via
`file tempfile`, which respects TMPDIR and avoids collisions
between parallel or repeated runs.  Move macports.conf and
sources.conf content into static template files with @TEST_ROOT@
substitution so the config structure is visible on disk rather
than generated inline.

Install a ::tcltest::cleanupTestsHook in the integration test
library so the temporary directory is removed via cleanupTests
even if a test omits its trailing explicit `cleanup` call.

Fix tests/test/arch-test/test.tcl, which invoked ${bindir}/port
via `sh -c` but never had bindir defined anywhere, to run
port.tcl directly through the test interpreter.

Replace hardcoded /tmp paths in portutil.test with $pwd-relative
paths.

Fixes: https://trac.macports.org/ticket/73828
Fixes: https://trac.macports.org/ticket/73827

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Herby Gillot
2026-04-11 12:09:14 -04:00
parent ffdb12fff0
commit 1084984798
40 changed files with 154 additions and 657 deletions
@@ -33,5 +33,6 @@
namespace eval macports::autoconf {
variable prefix "@prefix@"
variable os_platform "@OS_PLATFORM@"
variable test_tclsh "@TEST_TCLSH@"
variable top_srcdir "@abs_top_srcdir@"
}
+21 -5
View File
@@ -1,6 +1,21 @@
# Set up a custom environment with its own configuration.
# Shared by macports1.0, port1.0, and package1.0 test suites.
proc init_tmp_prefix {srcpath dstpath} {
# Create a throwaway MacPorts prefix at dstpath for unit testing.
#
# Generates a minimal macports.conf (pointing sources_conf at the
# single shared sources.conf in this directory), an empty
# variants.conf, and the directory layout that mportinit expects.
# Data files (pubkey, mtree specs, keys) are copied from the source
# tree so tests can run fully in-tree without an installed prefix.
#
# Sets PORTSRC so that a subsequent mportinit picks up the generated
# configuration.
#
# Shared by macports1.0, port1.0, and package1.0 test suites via
# test_setup.tcl.
#
# dstpath - Absolute path to the directory that will serve as the
# prefix. Created if it does not exist; callers should
# delete it when the test is finished.
proc init_tmp_prefix {dstpath} {
global env
umask 022
@@ -9,11 +24,13 @@ proc init_tmp_prefix {srcpath dstpath} {
makeDirectory $dstpath/share/macports/install
makeDirectory $dstpath/var/macports/registry
makeDirectory $dstpath/var/macports/distfiles
set top $macports::autoconf::top_srcdir
set sources_conf [file join $top src macports1.0 tests sources.conf]
set fd [open $dstpath/macports.conf w+]
puts $fd "portdbpath $dstpath/var/macports"
puts $fd "prefix $dstpath"
puts $fd "variants_conf $dstpath/variants.conf"
puts $fd "sources_conf $srcpath/sources.conf"
puts $fd "sources_conf $sources_conf"
puts $fd "applications_dir $dstpath/Applications"
puts $fd "frameworks_dir $dstpath/Library/Frameworks"
puts $fd "extra_env TCLLIBPATH"
@@ -21,7 +38,6 @@ proc init_tmp_prefix {srcpath dstpath} {
# Populate share/macports from the source tree instead of the
# installed prefix, so tests can run fully in-tree.
set top $macports::autoconf::top_srcdir
file copy $top/macports-pubkey.pem $dstpath/share/macports/
file copy $top/setupenv.bash $dstpath/share/macports/
file copy $top/doc/base.mtree $dstpath/share/macports/install/
-65
View File
@@ -1,65 +0,0 @@
##
# This is basically a copy of macports::worker_init, but without using
# sub-interpreters
proc macports_worker_init {} {
interp alias {} _cd {} cd
proc PortSystem {version} {
package require port $version
}
# Clearly separate slave interpreters and the master interpreter.
interp alias {} mport_exec {} mportexec
interp alias {} mport_open {} mportopen
interp alias {} mport_close {} mportclose
interp alias {} mport_lookup {} mportlookup
interp alias {} mport_info {} mportinfo
# Export some utility functions defined here.
interp alias {} macports_create_thread {} macports::create_thread
interp alias {} getportworkpath_from_buildpath {} macports::getportworkpath_from_buildpath
interp alias {} getportresourcepath {} macports::getportresourcepath
interp alias {} getportlogpath {} macports::getportlogpath
interp alias {} getdefaultportresourcepath {} macports::getdefaultportresourcepath
interp alias {} getprotocol {} macports::getprotocol
interp alias {} getportdir {} macports::getportdir
interp alias {} findBinary {} macports::findBinary
interp alias {} binaryInPath {} macports::binaryInPath
# New Registry/Receipts stuff
interp alias {} registry_new {} registry::new_entry
interp alias {} registry_open {} registry::open_entry
interp alias {} registry_write {} registry::write_entry
interp alias {} registry_prop_store {} registry::property_store
interp alias {} registry_prop_retr {} registry::property_retrieve
interp alias {} registry_exists {} registry::entry_exists
interp alias {} registry_exists_for_name {} registry::entry_exists_for_name
interp alias {} registry_activate {} portimage::activate
interp alias {} registry_deactivate {} portimage::deactivate
interp alias {} registry_deactivate_composite {} portimage::deactivate_composite
interp alias {} registry_uninstall {} registry_uninstall::uninstall
interp alias {} registry_register_deps {} registry::register_dependencies
interp alias {} registry_fileinfo_for_index {} registry::fileinfo_for_index
interp alias {} registry_fileinfo_for_file {} registry::fileinfo_for_file
interp alias {} registry_bulk_register_files {} registry::register_bulk_files
interp alias {} registry_active {} registry::active
interp alias {} registry_file_registered {} registry::file_registered
interp alias {} registry_port_registered {} registry::port_registered
interp alias {} registry_list_depends {} registry::list_depends
# deferred options processing.
interp alias {} getoption {} macports::getoption
# ping cache
interp alias {} get_pingtime {} macports::get_pingtime
interp alias {} set_pingtime {} macports::set_pingtime
# archive_sites.conf handling
interp alias {} get_archive_sites_conf_values {} macports::get_archive_sites_conf_values
foreach opt $macports::portinterp_options {
if {![info exists $opt]} {
global macports::$opt
set ::$opt macports::$opt
}
if {[info exists $opt]} {
set system_options($opt) $opt
set ::$opt $opt
}
}
# We don't need to handle portinterp_deferred_options, they're
# automatically handled correctly.
}
+1 -17
View File
@@ -7,26 +7,10 @@ namespace import tcltest::*
set pwd [file dirname [file normalize $argv0]]
source ../macports_test_autoconf.tcl
source $macports::autoconf::top_srcdir/src/macports1.0/tests/test_setup.tcl
# constraint for darwin platform
testConstraint darwin [expr {$macports::autoconf::os_platform eq "darwin"}]
package require macports 1.0
package require Thread
source ./library.tcl
source ./init_tmp_prefix.tcl
# clean leftovers from interrupted tests
file delete -force $pwd/tmpdir
# use a local, temporary prefix for testing
init_tmp_prefix $pwd $pwd/tmpdir
# Debug options
array set ui_options {}
set ui_options(ports_noninteractive) yes
mportinit ui_options
test mportclose {
Mport close unit test.
+1 -16
View File
@@ -6,22 +6,7 @@ namespace import tcltest::*
set pwd [file dirname [file normalize $argv0]]
source ../macports_test_autoconf.tcl
package require macports 1.0
source ./library.tcl
source ./init_tmp_prefix.tcl
# clean leftovers from interrupted tests
file delete -force $pwd/tmpdir
# use a local, temporary prefix for testing
init_tmp_prefix $pwd $pwd/tmpdir
# Debug options
array set ui_options {}
#set ui_options(ports_debug) yes
#set ui_options(ports_verbose) yes
set ui_options(ports_noninteractive) yes
mportinit ui_options
source $macports::autoconf::top_srcdir/src/macports1.0/tests/test_setup.tcl
source ../macports_dlist.tcl
+1 -14
View File
@@ -7,24 +7,11 @@ namespace import tcltest::*
set pwd [file dirname [file normalize $argv0]]
source ../macports_test_autoconf.tcl
source $macports::autoconf::top_srcdir/src/macports1.0/tests/test_setup.tcl
package require macports 1.0
package require Thread
package require Pextlib 1.0
source ./library.tcl
source ./init_tmp_prefix.tcl
# clean leftovers from interrupted tests
file delete -force $pwd/tmpdir
# use a local, temporary prefix for testing
init_tmp_prefix $pwd $pwd/tmpdir
# Debug options
array set ui_options {}
set ui_options(ports_noninteractive) yes
mportinit ui_options
# portutil.tcl defines a Tcl chown proc (wrapping lchown + fs-traverse)
# that overrides the Pextlib C chown command in port interpreters.
# port1.0 is not loaded here, so define it for testing.
+1 -13
View File
@@ -8,19 +8,7 @@ namespace import tcltest::*
set pwd [file dirname [file normalize $argv0]]
source ../macports_test_autoconf.tcl
package require macports 1.0
source ./library.tcl
source ./init_tmp_prefix.tcl
# clean leftovers from interrupted tests
file delete -force $pwd/tmpdir
# use a local, temporary prefix for testing
init_tmp_prefix $pwd $pwd/tmpdir
array set ui_options {}
set ui_options(ports_noninteractive) yes
mportinit ui_options
source $macports::autoconf::top_srcdir/src/macports1.0/tests/test_setup.tcl
set setup_testdir {
set saved_portdbpath ${macports::portdbpath}
@@ -1,6 +1,32 @@
# Shared test initialization for macports1.0, port1.0, and package1.0 tests.
#
# Prerequisites before sourcing this file:
# package require tcltest 2
# namespace import tcltest::*
# set pwd [file dirname [file normalize $argv0]]
# source ../XXX_test_autoconf.tcl ;# sets macports::autoconf::*
package require macports 1.0
# Source shared test helpers from this directory.
set _test_setup_dir [file dirname [file normalize [info script]]]
source [file join $_test_setup_dir init_tmp_prefix.tcl]
# Clean leftovers from interrupted tests and set up a temporary prefix.
file delete -force $pwd/tmpdir
init_tmp_prefix $pwd/tmpdir
array set ui_options {}
set ui_options(ports_noninteractive) yes
mportinit ui_options
# Constraint for darwin platform.
testConstraint darwin [expr {$tcl_platform(os) eq "Darwin"}]
##
# This is basically a copy of macports::worker_init, but without using
# sub-interpreters
# Consolidated copy of macports::worker_init for testing, without using
# sub-interpreters. Superset of aliases needed by macports1.0, port1.0,
# and package1.0 test suites.
proc macports_worker_init {} {
interp alias {} _cd {} cd
proc PortSystem {version} {
@@ -24,6 +50,7 @@ proc macports_worker_init {} {
interp alias {} getportdir {} macports::getportdir
interp alias {} findBinary {} macports::findBinary
interp alias {} binaryInPath {} macports::binaryInPath
interp alias {} shellescape {} macports::shellescape
# New Registry/Receipts stuff
interp alias {} registry_new {} registry::new_entry
interp alias {} registry_open {} registry::open_entry
@@ -32,6 +32,7 @@
namespace eval macports::autoconf {
variable prefix "@prefix@"
variable test_tclsh "@TEST_TCLSH@"
variable top_srcdir "@abs_top_srcdir@"
}
-76
View File
@@ -42,79 +42,3 @@ proc env_init {} {
}
}
##
# This is basically a copy of macports::worker_init, but without using
# sub-interpreters
proc macports_worker_init {} {
interp alias {} _cd {} cd
proc PortSystem {version} {
package require port $version
}
# Clearly separate slave interpreters and the master interpreter.
interp alias {} mport_exec {} mportexec
interp alias {} mport_open {} mportopen
interp alias {} mport_close {} mportclose
interp alias {} mport_lookup {} mportlookup
interp alias {} mport_info {} mportinfo
# Export some utility functions defined here.
interp alias {} macports_create_thread {} macports::create_thread
interp alias {} getportworkpath_from_buildpath {} macports::getportworkpath_from_buildpath
interp alias {} getportresourcepath {} macports::getportresourcepath
interp alias {} getportlogpath {} macports::getportlogpath
interp alias {} getdefaultportresourcepath {} macports::getdefaultportresourcepath
interp alias {} getprotocol {} macports::getprotocol
interp alias {} getportdir {} macports::getportdir
interp alias {} findBinary {} macports::findBinary
interp alias {} binaryInPath {} macports::binaryInPath
interp alias {} shellescape {} macports::shellescape
# New Registry/Receipts stuff
interp alias {} registry_new {} registry::new_entry
interp alias {} registry_open {} registry::open_entry
interp alias {} registry_write {} registry::write_entry
interp alias {} registry_prop_store {} registry::property_store
interp alias {} registry_prop_retr {} registry::property_retrieve
interp alias {} registry_exists {} registry::entry_exists
interp alias {} registry_exists_for_name {} registry::entry_exists_for_name
interp alias {} registry_activate {} portimage::activate
interp alias {} registry_deactivate {} portimage::deactivate
interp alias {} registry_deactivate_composite {} portimage::deactivate_composite
interp alias {} registry_uninstall {} registry_uninstall::uninstall
interp alias {} registry_register_deps {} registry::register_dependencies
interp alias {} registry_fileinfo_for_index {} registry::fileinfo_for_index
interp alias {} registry_fileinfo_for_file {} registry::fileinfo_for_file
interp alias {} registry_bulk_register_files {} registry::register_bulk_files
interp alias {} registry_active {} registry::active
interp alias {} registry_file_registered {} registry::file_registered
interp alias {} registry_port_registered {} registry::port_registered
interp alias {} registry_list_depends {} registry::list_depends
# deferred options processing.
interp alias {} getoption {} macports::getoption
# ping cache
interp alias {} get_pingtime {} macports::get_pingtime
interp alias {} set_pingtime {} macports::set_pingtime
# archive_sites.conf handling
interp alias {} get_archive_sites_conf_values {} macports::get_archive_sites_conf_values
foreach opt $macports::portinterp_options {
if {![info exists $opt]} {
global macports::$opt
set ::$opt macports::$opt
}
if {[info exists $opt]} {
set system_options($opt) $opt
set ::$opt $opt
}
}
# We don't need to handle portinterp_deferred_options, they're
# automatically handled correctly.
}
testConstraint darwin [expr {$tcl_platform(os) eq "Darwin"}]
+1 -14
View File
@@ -6,21 +6,8 @@ namespace import tcltest::*
set pwd [file dirname [file normalize $argv0]]
source ../package_test_autoconf.tcl
package require macports 1.0
source $macports::autoconf::top_srcdir/src/macports1.0/tests/test_setup.tcl
source ./library.tcl
source $macports::autoconf::top_srcdir/src/macports1.0/tests/init_tmp_prefix.tcl
# clean leftovers from interrupted tests
file delete -force $pwd/tmpdir
# use a local, temporary prefix for testing
init_tmp_prefix $pwd $pwd/tmpdir
array set ui_options {}
#set ui_options(ports_debug) yes
#set ui_options(ports_verbose) yes
set ui_options(ports_noninteractive) yes
mportinit ui_options
macports_worker_init
+1 -14
View File
@@ -6,21 +6,8 @@ namespace import tcltest::*
set pwd [file dirname [file normalize $argv0]]
source ../package_test_autoconf.tcl
package require macports 1.0
source $macports::autoconf::top_srcdir/src/macports1.0/tests/test_setup.tcl
source ./library.tcl
source $macports::autoconf::top_srcdir/src/macports1.0/tests/init_tmp_prefix.tcl
# clean leftovers from interrupted tests
file delete -force $pwd/tmpdir
# use a local, temporary prefix for testing
init_tmp_prefix $pwd $pwd/tmpdir
array set ui_options {}
#set ui_options(ports_debug) yes
#set ui_options(ports_verbose) yes
set ui_options(ports_noninteractive) yes
mportinit ui_options
package require portdmg 1.0
package require portpkg 1.0
+1 -14
View File
@@ -6,21 +6,8 @@ namespace import tcltest::*
set pwd [file dirname [file normalize $argv0]]
source ../package_test_autoconf.tcl
package require macports 1.0
source $macports::autoconf::top_srcdir/src/macports1.0/tests/test_setup.tcl
source ./library.tcl
source $macports::autoconf::top_srcdir/src/macports1.0/tests/init_tmp_prefix.tcl
# clean leftovers from interrupted tests
file delete -force $pwd/tmpdir
# use a local, temporary prefix for testing
init_tmp_prefix $pwd $pwd/tmpdir
array set ui_options {}
#set ui_options(ports_debug) yes
#set ui_options(ports_verbose) yes
set ui_options(ports_noninteractive) yes
mportinit ui_options
macports_worker_init
+1 -14
View File
@@ -6,21 +6,8 @@ namespace import tcltest::*
set pwd [file dirname [file normalize $argv0]]
source ../package_test_autoconf.tcl
package require macports 1.0
source $macports::autoconf::top_srcdir/src/macports1.0/tests/test_setup.tcl
source ./library.tcl
source $macports::autoconf::top_srcdir/src/macports1.0/tests/init_tmp_prefix.tcl
# clean leftovers from interrupted tests
file delete -force $pwd/tmpdir
# use a local, temporary prefix for testing
init_tmp_prefix $pwd $pwd/tmpdir
array set ui_options {}
#set ui_options(ports_debug) yes
#set ui_options(ports_verbose) yes
set ui_options(ports_noninteractive) yes
mportinit ui_options
package require portmpkg 1.0
package require portpkg 1.0
+1 -14
View File
@@ -6,21 +6,8 @@ namespace import tcltest::*
set pwd [file dirname [file normalize $argv0]]
source ../package_test_autoconf.tcl
package require macports 1.0
source $macports::autoconf::top_srcdir/src/macports1.0/tests/test_setup.tcl
source ./library.tcl
source $macports::autoconf::top_srcdir/src/macports1.0/tests/init_tmp_prefix.tcl
# clean leftovers from interrupted tests
file delete -force $pwd/tmpdir
# use a local, temporary prefix for testing
init_tmp_prefix $pwd $pwd/tmpdir
array set ui_options {}
#set ui_options(ports_debug) yes
#set ui_options(ports_verbose) yes
set ui_options(ports_noninteractive) yes
mportinit ui_options
package require portmpkg 1.0
package require portpkg 1.0
-27
View File
@@ -1,27 +0,0 @@
# MacPorts system-wide configuration file for ports tree sources.
#
# To change how MacPorts fetches base, see rsync_server and rsync_dir in
# macports.conf.
# To add a local source, add a "file://" entry.
#
# Example: file:///Users/landonf/misc/MacPorts/ports
#
# To prevent a source from synchronizing when `port sync` is used,
# append "[nosync]" at the end.
#
# Example: file:///Users/landonf/misc/MacPorts/ports [nosync]
#
# Note that MacPorts parses source URLs in order; when a port appears in
# multiple sources, it installs the first occurrence. For local sources
# to shadow remote ones, "file://" URLs must come before other URLs.
# A list of rsync mirrors is available at
# https://trac.macports.org/wiki/Mirrors#Portfiles.
#
# If an "rsync://" URL points to a .tar file, a signed .rmd160 file must
# exist in the same directory on the server and will be used to verify
# its integrity.
#
# For proper functionality of various resources (port groups, mirror
# sites, etc.), the primary MacPorts source must always be tagged
# "[default]", even if switched from the default "rsync://" URL.
rsync://rsync.macports.org/macports/release/tarballs/ports.tar [default]
+1
View File
@@ -32,5 +32,6 @@
namespace eval macports::autoconf {
variable prefix "@prefix@"
variable test_tclsh "@TEST_TCLSH@"
variable top_srcdir "@abs_top_srcdir@"
}
+1 -15
View File
@@ -6,21 +6,7 @@ namespace import tcltest::*
set pwd [file dirname [file normalize $argv0]]
source ../port_test_autoconf.tcl
package require macports 1.0
source ./library.tcl
source $macports::autoconf::top_srcdir/src/macports1.0/tests/init_tmp_prefix.tcl
# clean leftovers from interrupted tests
file delete -force $pwd/tmpdir
# use a local, temporary prefix for testing
init_tmp_prefix $pwd $pwd/tmpdir
array set ui_options {}
#set ui_options(ports_debug) yes
#set ui_options(ports_verbose) yes
set ui_options(ports_noninteractive) yes
mportinit ui_options
source $macports::autoconf::top_srcdir/src/macports1.0/tests/test_setup.tcl
package require portactivate 1.0
package require registry 1.0
+1 -15
View File
@@ -6,21 +6,7 @@ namespace import tcltest::*
set pwd [file dirname [file normalize $argv0]]
source ../port_test_autoconf.tcl
package require macports 1.0
source ./library.tcl
source $macports::autoconf::top_srcdir/src/macports1.0/tests/init_tmp_prefix.tcl
# clean leftovers from interrupted tests
file delete -force $pwd/tmpdir
# use a local, temporary prefix for testing
init_tmp_prefix $pwd $pwd/tmpdir
array set ui_options {}
#set ui_options(ports_debug) yes
#set ui_options(ports_verbose) yes
set ui_options(ports_noninteractive) yes
mportinit ui_options
source $macports::autoconf::top_srcdir/src/macports1.0/tests/test_setup.tcl
# Provide a stub for the port callback mechanism
namespace eval port {
+1 -13
View File
@@ -6,19 +6,7 @@ namespace import tcltest::*
set pwd [file dirname [file normalize $argv0]]
source ../port_test_autoconf.tcl
package require macports 1.0
source ./library.tcl
source $macports::autoconf::top_srcdir/src/macports1.0/tests/init_tmp_prefix.tcl
# clean leftovers from interrupted tests
file delete -force $pwd/tmpdir
# use a local, temporary prefix for testing
init_tmp_prefix $pwd $pwd/tmpdir
array set ui_options {}
set ui_options(ports_noninteractive) yes
mportinit ui_options
source $macports::autoconf::top_srcdir/src/macports1.0/tests/test_setup.tcl
# Provide a stub for the port callback mechanism
namespace eval port {

Some files were not shown because too many files have changed in this diff Show More