Skip to content

cmd/link: TestLargeText in linkbig_test.go does not look right #45406

Closed
@perillo

Description

@perillo

What version of Go are you using (go version)?

$ go version
go version go1.16.3 linux/amd64

The TestLargeText seems to have some problems:

  1. It calls os.Chdir without checking the error
  2. Does not restore the original working directory
  3. Calls os.Chdir later, instead of calling it early (allowing a simplification of the code)
  4. Calls os.Chdir(tmpdir) twice

Here is a patch that will allow the use of the chtmpdir function (see #45182).
os.Chdir error handling and working directory restoration is not included.
It uses relative paths, since we have chdir early.

diff --git a/src/cmd/link/linkbig_test.go b/src/cmd/link/linkbig_test.go
index d5d77d6c72..e6f58a5843 100644
--- a/src/cmd/link/linkbig_test.go
+++ b/src/cmd/link/linkbig_test.go
@@ -28,6 +28,7 @@ func TestLargeText(t *testing.T) {
 	var w bytes.Buffer
 	const FN = 4
 	tmpdir := t.TempDir()
+	os.Chdir(tmpdir)
 
 	// Generate the scenario where the total amount of text exceeds the
 	// limit for the jmp/call instruction, on RISC architectures like ppc64le,
@@ -47,7 +48,7 @@ func TestLargeText(t *testing.T) {
 			fmt.Fprintf(&w, inst)
 		}
 		fmt.Fprintf(&w, "\tRET\n")
-		err := ioutil.WriteFile(tmpdir+"/"+testname+".s", w.Bytes(), 0666)
+		err := ioutil.WriteFile(testname+".s", w.Bytes(), 0666)
 		if err != nil {
 			t.Fatalf("can't write output: %v\n", err)
 		}
@@ -74,32 +75,30 @@ func TestLargeText(t *testing.T) {
 	fmt.Fprintf(&w, "\t}\n")
 	fmt.Fprintf(&w, "\tfmt.Printf(\"PASS\\n\")\n")
 	fmt.Fprintf(&w, "}")
-	err := ioutil.WriteFile(tmpdir+"/bigfn.go", w.Bytes(), 0666)
+	err := ioutil.WriteFile("bigfn.go", w.Bytes(), 0666)
 	if err != nil {
 		t.Fatalf("can't write output: %v\n", err)
 	}
 
 	// Build and run with internal linking.
-	os.Chdir(tmpdir)
 	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", "bigtext")
 	out, err := cmd.CombinedOutput()
 	if err != nil {
 		t.Fatalf("Build failed for big text program with internal linking: %v, output: %s", err, out)
 	}
-	cmd = exec.Command(tmpdir + "/bigtext")
+	cmd = exec.Command("./bigtext")
 	out, err = cmd.CombinedOutput()
 	if err != nil {
 		t.Fatalf("Program built with internal linking failed to run with err %v, output: %s", err, out)
 	}
 
 	// Build and run with external linking
-	os.Chdir(tmpdir)
 	cmd = exec.Command(testenv.GoToolPath(t), "build", "-o", "bigtext", "-ldflags", "'-linkmode=external'")
 	out, err = cmd.CombinedOutput()
 	if err != nil {
 		t.Fatalf("Build failed for big text program with external linking: %v, output: %s", err, out)
 	}
-	cmd = exec.Command(tmpdir + "/bigtext")
+	cmd = exec.Command("./bigtext")
 	out, err = cmd.CombinedOutput()
 	if err != nil {
 		t.Fatalf("Program built with external linking failed to run with err %v, output: %s", err, out)

Commenting the code that skip the test on my system (linux amd64), the test works, but I'm not sure if there was a good reason for the current implementation.

However I have found an additional issue: the test only works if GO111MODULE=off, otherwise the test fails with

Build failed for big text program with internal linking: exit status 1, output: go: go.mod file not found in current directory or any parent directory; see 'go help modules'

and all the tests that follow (when executing run.bash) fails with

cannot determine current directory: getwd: no such file or directory

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.TestingAn issue that has been verified to require only test changes, not just a test failure.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions