Skip to content

Qualify objective() in its own tests, which ROI masks in a suite run - #61

Merged
braverock merged 1 commit into
braverock:masterfrom
sstoeckl:fix-objective-test-masked-by-roi
Sep 19, 2026
Merged

braverock merged 1 commit into
braverock:masterfrom
sstoeckl:fix-objective-test-masked-by-roi

Conversation

@sstoeckl

Copy link
Copy Markdown
Contributor

tests/testthat/test-objective-branches.R passes when it is run on its own and
fails when the suite is run. It is not the package: by the time the file runs,
objective() is a different function.

ROI exports objective() as well. Twenty-seven of the test files call
library(ROI), and library() does not move an already-attached package back
up the search path, so a later library(PortfolioAnalytics) does not restore
it. The tests then call ROI's objective(), which takes a single argument x:

Error in objective(name = "mean", arguments = list()) :
  unused arguments (name = "mean", arguments = list())

Measured

Same file, same package build, one library() call apart:

file library(ROI) first PASS FAIL ERROR
master no 33 0 0
master yes 30 0 2
this branch yes 33 0 0
this branch no 33 0 0

The change

Three calls become PortfolioAnalytics::objective(...), with a comment saying
why, so the file tests the function it is written for regardless of what a
previous file attached.

The expected messages are pinned at the same time, because one of the three
tests passed under masking without testing anything: it expected the regexp
"name", and name also appears in R's own unused arguments (name = NULL, arguments = list()). It now expects the sentence the function actually raises.

Nothing in R/ changes.

Test suite

Run against master's package code, so the only difference is this file:

PASS FAIL ERROR
master 3550 2 2
this branch 3553 2 0

No errors left. The two failures are the chart.EfficientFrontierCompare()
tests, which #60 fixes; measured there, that file goes from 67 passing
with 2 failures to 69 with none. The two changes touch different files, so
between them the four problems run-all.R reports are accounted for.

Why this one and #60 are worth taking first

R-CMD-check and test-coverage have both been red on master since 24 July,
and both die at the same step, running the tests in 'run-all.R' failed. What
run-all.R reports there is four problems: the two in this file, and two in
test-charts-roi.R that come from a missing initialisation in
chart.EfficientFrontierCompare(). #60 fixes those.

With both merged, that step passes and the pipeline is green again. I can only
see that the workflows stop at that step, so if something else is broken behind
it, it is hidden by these four rather than absent.

This matters for the five pull requests that are already open, because their
checks are red for this reason and not for theirs. They do not need rebasing:
the workflows run on pull_request and actions/checkout@v4 checks out the
merge commit, so once master carries these two fixes, re-running the jobs on
each of them is enough.

Scope

One test file, 18 insertions, 5 deletions, of which 12 insertions are the
comment. This change stands on its own.

🤖 Generated with Claude Code

test-objective-branches.R tests PortfolioAnalytics::objective(). ROI exports a
function of the same name, 27 of the test files attach ROI, and library() does
not move an already-attached package back up the search path. By the time this
file runs in a full suite, objective() is ROI's, which takes a single argument
x, so the tests exercise the wrong function.

Measured, same file, same package build, one library() call apart:

  file      ROI attached   PASS  FAIL  ERROR
  master    no               33     0      0
  master    yes              30     0      2
  patched   yes              33     0      0
  patched   no               33     0      0

  Error in objective(name = "mean", arguments = list()):
    unused arguments (name = "mean", arguments = list())

This is one of the two reasons the run-all.R step fails on master, and the
reason it cannot be reproduced by running the file on its own.

Three calls are qualified. The expected messages are pinned at the same time,
because the first test passed under masking without testing anything: it
matched the regexp "name", and "name" also appears in R's own "unused arguments
(name = NULL, arguments = list())". It now expects the message the function
actually raises.

Nothing in R/ changes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The reviewed test changes address the suite-order failure without unresolved issues.

Pull request overview

This PR makes objective branch tests independent of ROI package masking.

Changes:

  • Qualifies calls as PortfolioAnalytics::objective().
  • Pins expected error messages.
  • Documents the package masking issue.
File summaries
File Description
tests/testthat/test-objective-branches.R Ensures objective tests consistently exercise the intended function.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@braverock
braverock merged commit c83bf85 into braverock:master Sep 19, 2026
@braverock

Copy link
Copy Markdown
Owner

reviewed and merged. Thank you for the contribution to PortfolioAnalytics @sstoeckl !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants