<commit_before>package cmd

import (
	"fmt"
	"github.com/drud/ddev/pkg/ddevapp"
	"github.com/drud/ddev/pkg/exec"
	"github.com/drud/ddev/pkg/fileutil"
	"github.com/drud/ddev/pkg/globalconfig"
	"github.com/drud/ddev/pkg/nodeps"
	"github.com/drud/ddev/pkg/testcommon"
	asrt "github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"os"
	"path/filepath"
	"runtime"
	"testing"
)

// TestHomeadditions makes sure that extra files added to
// .ddev/homeadditions and ~/.ddev/homeadditions get added into the container's ~/
func TestHomeadditions(t *testing.T) {
	if nodeps.MutagenEnabledDefault || globalconfig.DdevGlobalConfig.MutagenEnabledGlobal || nodeps.NoBindMountsDefault {
		t.Skip("Skipping because this changes homedir and breaks mutagen functionality")
	}
	assert := asrt.New(t)

	origDir, _ := os.Getwd()
	testdata := filepath.Join(origDir, "testdata", t.Name())

	tmpHome := testcommon.CreateTmpDir(t.Name() + "tempHome")
	origHome := os.Getenv("HOME")
	if runtime.GOOS == "windows" {
		origHome = os.Getenv("USERPROFILE")
	}
	// Change the homedir temporarily
	_ = os.Setenv("HOME", tmpHome)
	_ = os.Setenv("USERPROFILE", tmpHome)

	site := TestSites[0]
	projectHomeadditionsDir := filepath.Join(site.Dir, ".ddev", "homeadditions")

	// We can't use the standard getGlobalDDevDir here because *our* global hasn't changed.
	// It's changed via $HOME for the ddev subprocess
	err := os.MkdirAll(filepath.Join(tmpHome, ".ddev"), 0755)
	assert.NoError(err)
	tmpHomeGlobalHomeadditionsDir := filepath.Join(tmpHome, ".ddev", "homeadditions")
	err = os.RemoveAll(tmpHomeGlobalHomeadditionsDir)
	assert.NoError(err)
	err = os.RemoveAll(projectHomeadditionsDir)
	assert.NoError(err)
	err = fileutil.CopyDir(filepath.Join(testdata, "global"), tmpHomeGlobalHomeadditionsDir)
	assert.NoError(err)
	err = fileutil.CopyDir(filepath.Join(testdata, "project"), projectHomeadditionsDir)
	assert.NoError(err)
	err = os.Chdir(site.Dir)
	require.NoError(t, err)
	t.Cleanup(func() {
		_, err := os.Stat(globalconfig.GetMutagenPath())
		if err == nil {
			out, err := exec.RunHostCommand(DdevBin, "debug", "mutagen", "daemon", "stop")
			assert.NoError(err, "mutagen daemon stop returned %s", string(out))
		}

		err = os.Chdir(origDir)
		assert.NoError(err)
		err = fileutil.PurgeDirectory(projectHomeadditionsDir)
		assert.NoError(err)
		err = os.RemoveAll(tmpHome)
		assert.NoError(err)
		_ = os.Setenv("HOME", origHome)
		_ = os.Setenv("USERPROFILE", origHome)
	})

	// Before we can symlink global, need to make sure anything is already gone
	err = os.RemoveAll(filepath.Join(tmpHomeGlobalHomeadditionsDir, "realglobaltarget.txt"))
	assert.NoError(err)
	err = os.RemoveAll(filepath.Join(projectHomeadditionsDir, "realprojecttarget.txt"))
	assert.NoError(err)

	// symlink the project file
	err = os.Symlink(filepath.Join(origDir, "testdata", t.Name(), "project/realprojecttarget.txt"), filepath.Join(projectHomeadditionsDir, "realprojecttarget.txt"))
	require.NoError(t, err)
	// symlink the global file
	err = os.Symlink(filepath.Join(origDir, "testdata", t.Name(), "global/realglobaltarget.txt"), filepath.Join(tmpHomeGlobalHomeadditionsDir, "realglobaltarget.txt"))
	require.NoError(t, err)
	// Run ddev start make sure homeadditions example files get populated
	_, err = exec.RunHostCommand(DdevBin, "start")
	assert.NoError(err)

	for _, f := range []string{"bash_aliases.example", "README.txt"} {
		assert.FileExists(filepath.Join(projectHomeadditionsDir, f))
		assert.FileExists(filepath.Join(tmpHomeGlobalHomeadditionsDir, f))
	}

	app, err := ddevapp.GetActiveApp(site.Name)
	require.NoError(t, err)
	// Make sure that even though there was a global and a project-level .myscript.sh
	// the project-level one should win.
	stdout, _, err := app.Exec(&ddevapp.ExecOpts{
		Service: "web",
		Cmd:     "~/.myscript.sh",<commit_msg>[tests only] Fix Windows NFS homeadditions failure (#4104)<commit_after>package cmd

import (
	"fmt"
	"github.com/drud/ddev/pkg/ddevapp"
	"github.com/drud/ddev/pkg/exec"
	"github.com/drud/ddev/pkg/fileutil"
	"github.com/drud/ddev/pkg/globalconfig"
	"github.com/drud/ddev/pkg/nodeps"
	"github.com/drud/ddev/pkg/testcommon"
	asrt "github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"os"
	"path/filepath"
	"runtime"
	"testing"
)

// TestHomeadditions makes sure that extra files added to
// .ddev/homeadditions and ~/.ddev/homeadditions get added into the container's ~/
func TestHomeadditions(t *testing.T) {
	if nodeps.MutagenEnabledDefault || globalconfig.DdevGlobalConfig.MutagenEnabledGlobal || nodeps.NoBindMountsDefault {
		t.Skip("Skipping because this changes homedir and breaks mutagen functionality")
	}
	assert := asrt.New(t)

	origDir, _ := os.Getwd()
	testdata := filepath.Join(origDir, "testdata", t.Name())

	tmpHome := testcommon.CreateTmpDir(t.Name() + "tempHome")
	origHome := os.Getenv("HOME")
	if runtime.GOOS == "windows" {
		origHome = os.Getenv("USERPROFILE")
	}
	// Change the homedir temporarily
	_ = os.Setenv("HOME", tmpHome)
	_ = os.Setenv("USERPROFILE", tmpHome)

	site := TestSites[0]
	projectHomeadditionsDir := filepath.Join(site.Dir, ".ddev", "homeadditions")

	// We can't use the standard getGlobalDDevDir here because *our* global hasn't changed.
	// It's changed via $HOME for the ddev subprocess
	err := os.MkdirAll(filepath.Join(tmpHome, ".ddev"), 0755)
	assert.NoError(err)
	tmpHomeGlobalHomeadditionsDir := filepath.Join(tmpHome, ".ddev", "homeadditions")
	err = os.RemoveAll(tmpHomeGlobalHomeadditionsDir)
	assert.NoError(err)
	err = os.RemoveAll(projectHomeadditionsDir)
	assert.NoError(err)
	err = fileutil.CopyDir(filepath.Join(testdata, "global"), tmpHomeGlobalHomeadditionsDir)
	assert.NoError(err)
	err = fileutil.CopyDir(filepath.Join(testdata, "project"), projectHomeadditionsDir)
	assert.NoError(err)
	err = os.Chdir(site.Dir)
	require.NoError(t, err)
	t.Cleanup(func() {
		_, err := os.Stat(globalconfig.GetMutagenPath())
		if err == nil {
			out, err := exec.RunHostCommand(DdevBin, "debug", "mutagen", "daemon", "stop")
			assert.NoError(err, "mutagen daemon stop returned %s", string(out))
		}

		err = os.Chdir(origDir)
		assert.NoError(err)
		err = fileutil.PurgeDirectory(projectHomeadditionsDir)
		assert.NoError(err)
		err = os.RemoveAll(tmpHome)
		assert.NoError(err)
		_ = os.Setenv("HOME", origHome)
		_ = os.Setenv("USERPROFILE", origHome)
	})

	// Before we can symlink global, need to make sure anything is already gone
	err = os.RemoveAll(filepath.Join(tmpHomeGlobalHomeadditionsDir, "realglobaltarget.txt"))
	assert.NoError(err)
	err = os.RemoveAll(filepath.Join(projectHomeadditionsDir, "realprojecttarget.txt"))
	assert.NoError(err)

	// symlink the project file
	err = os.Symlink(filepath.Join(origDir, "testdata", t.Name(), "project/realprojecttarget.txt"), filepath.Join(projectHomeadditionsDir, "realprojecttarget.txt"))
	require.NoError(t, err)
	// symlink the global file
	err = os.Symlink(filepath.Join(origDir, "testdata", t.Name(), "global/realglobaltarget.txt"), filepath.Join(tmpHomeGlobalHomeadditionsDir, "realglobaltarget.txt"))
	require.NoError(t, err)
	// Run ddev start make sure homeadditions example files get populated
	_, err = exec.RunHostCommand(DdevBin, "restart")
	assert.NoError(err)

	for _, f := range []string{"bash_aliases.example", "README.txt"} {
		assert.FileExists(filepath.Join(projectHomeadditionsDir, f))
		assert.FileExists(filepath.Join(tmpHomeGlobalHomeadditionsDir, f))
	}

	app, err := ddevapp.GetActiveApp(site.Name)
	require.NoError(t, err)

	// Make sure that even though there was a global and a project-level .myscript.sh
	// the project-level one should win.

	stdout, _, err := app.Exec(&ddevapp.ExecOpts{
		Service: "web",
		Cmd:     "~/.myscript.sh",
{'fraction_non_alphanumeric': 0.14123271740534954, 'fraction_numerical': 0.0018090192531334797, 'mean_word_length': 5.211878009630818, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '909369', 'n_tokens_mistral': 2830, 'n_tokens_neox': 2760, 'n_words': 532}