Commit 184ac0d2 authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

fix install.xq execution + paths change

parent c2e57dca
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -1432,6 +1432,7 @@ func syncBundles(rootDirectory string) error {
				filepath.Join(bundleDir, "install.xq"),
			}
			cmd := exec.Command(javaBin, args...)
			cmd.Dir = rootDirectory
			cmd.Stdout = os.Stdout
			cmd.Stderr = os.Stderr
			if err := cmd.Run(); err != nil {
@@ -1461,15 +1462,22 @@ func copyMaxTree(dotMaxDir, maxReleaseName, maxReleaseURL string) error {
	}

	root := filepath.Join(tmpDir, fmt.Sprintf("max-v2-%s", maxReleaseName))
	if err := copyDir(filepath.Join(root, "src", "main", "core"), filepath.Join(dotMaxDir, "basex", "webapp", "max", "core")); err != nil {
	return copyMaxTreeFromRoot(root, dotMaxDir)
}

func copyMaxTreeFromRoot(root, dotMaxDir string) error {
	if err := copyDir(filepath.Join(root, "src", "main", "core"), filepath.Join(dotMaxDir, "basex", "repo", "max", "max")); err != nil {
		return err
	}
	if err := copyDir(filepath.Join(root, "src", "main", "bundles"), filepath.Join(dotMaxDir, "basex", "webapp", "max", "bundles")); err != nil {
	if err := copyDir(filepath.Join(root, "src", "main", "webapp", "max"), filepath.Join(dotMaxDir, "basex", "webapp", "max")); err != nil {
		return err
	}
	if err := copyDir(filepath.Join(root, "src", "resources"), filepath.Join(dotMaxDir, "resources")); err != nil {
		return err
	}
	if err := copyFile(filepath.Join(root, "src", "resources", "expath-pkg.xml"), filepath.Join(dotMaxDir, "basex", "repo", "max", "expath-pkg.xml")); err != nil {
		return err
	}
	return nil
}

+103 −0
Original line number Diff line number Diff line
@@ -161,6 +161,43 @@ func TestFilterSupportedMaxReleasesDropsVersionsBelowMinimum(t *testing.T) {
	}
}

func TestCopyMaxTreeFromRootUsesSupportedReleaseLayout(t *testing.T) {
	root := t.TempDir()
	dotMaxDir := filepath.Join(t.TempDir(), ".max")

	paths := []string{
		filepath.Join(root, "src", "main", "core", "conf.xqm"),
		filepath.Join(root, "src", "main", "webapp", "max", "routes.xqm"),
		filepath.Join(root, "src", "main", "webapp", "max", "bundles", "max-dumb-xml", "install.xq"),
		filepath.Join(root, "src", "resources", "available-bundles.xml"),
		filepath.Join(root, "src", "resources", "expath-pkg.xml"),
	}
	for _, p := range paths {
		if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
			t.Fatal(err)
		}
		if err := os.WriteFile(p, []byte(filepath.Base(p)), 0o644); err != nil {
			t.Fatal(err)
		}
	}

	if err := copyMaxTreeFromRoot(root, dotMaxDir); err != nil {
		t.Fatal(err)
	}

	for _, p := range []string{
		filepath.Join(dotMaxDir, "basex", "repo", "max", "max", "conf.xqm"),
		filepath.Join(dotMaxDir, "basex", "repo", "max", "expath-pkg.xml"),
		filepath.Join(dotMaxDir, "basex", "webapp", "max", "routes.xqm"),
		filepath.Join(dotMaxDir, "basex", "webapp", "max", "bundles", "max-dumb-xml", "install.xq"),
		filepath.Join(dotMaxDir, "resources", "available-bundles.xml"),
	} {
		if !fileExists(p) {
			t.Fatalf("expected copied path to exist: %s", p)
		}
	}
}

func TestExtractTarGzPreservesExecutableBit(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip("executable permission bits are not relevant on windows")
@@ -375,6 +412,69 @@ func TestFeedDirectoryConfirmationCanAbort(t *testing.T) {
	}
}

func TestBundleInstallRunsFromProjectDirectory(t *testing.T) {
	tmpHome := t.TempDir()
	prevHome := userMaxDir
	prevCache := cacheDir
	userMaxDir = tmpHome
	cacheDir = filepath.Join(userMaxDir, "cache")
	t.Cleanup(func() {
		userMaxDir = prevHome
		cacheDir = prevCache
	})

	oldWD, err := os.Getwd()
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() {
		_ = os.Chdir(oldWD)
	})

	projectDir := filepath.Join(t.TempDir(), "project")
	if err := os.MkdirAll(projectDir, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(projectDir, maxConfigFile), []byte(configInitTemplate), 0o644); err != nil {
		t.Fatal(err)
	}

	bundlesDir := filepath.Join(projectDir, ".max", "basex", "webapp", "max", "bundles")
	for _, name := range []string{"max-dumb-xml", "max-export", "max-dev"} {
		if err := os.MkdirAll(filepath.Join(bundlesDir, name), 0o755); err != nil {
			t.Fatal(err)
		}
	}
	installPath := filepath.Join(bundlesDir, "max-dumb-xml", "install.xq")
	if err := os.WriteFile(installPath, []byte("()"), 0o644); err != nil {
		t.Fatal(err)
	}

	logPath := filepath.Join(t.TempDir(), "pwd.log")
	t.Setenv("CLIMAX_TEST_JAVA_PWD_LOG", logPath)
	if err := installFakeBundledJava(tmpHome); err != nil {
		t.Fatal(err)
	}
	if err := os.Chdir(t.TempDir()); err != nil {
		t.Fatal(err)
	}

	if err := syncBundles(projectDir); err != nil {
		t.Fatal(err)
	}

	lines, err := readLogLines(logPath)
	if err != nil {
		t.Fatal(err)
	}
	if len(lines) != 1 {
		t.Fatalf("expected one install invocation, got %d: %v", len(lines), lines)
	}
	if lines[0] != projectDir {
		t.Fatalf("expected bundle install working directory %s, got %s", projectDir, lines[0])
	}
}

func installFakeBundledJava(home string) error {
	javaBin := bundledJavaPath(filepath.Join(home, "jdk"), currentSystem())
	if javaBin == "" {
@@ -386,6 +486,9 @@ func installFakeBundledJava(home string) error {
	script := "#!/bin/sh\n" +
		"if [ -n \"$CLIMAX_TEST_JAVA_LOG\" ]; then\n" +
		"  printf '%s\\n' \"$*\" >> \"$CLIMAX_TEST_JAVA_LOG\"\n" +
		"fi\n" +
		"if [ -n \"$CLIMAX_TEST_JAVA_PWD_LOG\" ]; then\n" +
		"  pwd >> \"$CLIMAX_TEST_JAVA_PWD_LOG\"\n" +
		"fi\n"
	return os.WriteFile(javaBin, []byte(script), 0o755)
}