<commit_before>package pluginaction_test

import (
	"errors"
	"io/ioutil"
	"os"
	"path/filepath"

	. "code.cloudfoundry.org/cli/actor/pluginaction"
	"code.cloudfoundry.org/cli/actor/pluginaction/pluginactionfakes"
	"code.cloudfoundry.org/cli/util/configv3"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("install actions", func() {
	var (
		actor      Actor
		fakeConfig *pluginactionfakes.FakeConfig
		tempDir    string
	)

	BeforeEach(func() {
		fakeConfig = new(pluginactionfakes.FakeConfig)
		var err error
		tempDir, err = ioutil.TempDir("", "")
		Expect(err).ToNot(HaveOccurred())
		fakeConfig.PluginHomeReturns(tempDir)
		actor = NewActor(fakeConfig, nil)
	})

	AfterEach(func() {
		err := os.RemoveAll(tempDir)
		Expect(err).ToNot(HaveOccurred())
	})

	Describe("CreateExecutableCopy", func() {
		Context("when the file exists", func() {
			var pluginPath string

			BeforeEach(func() {
				tempFile, err := ioutil.TempFile("", "")
				Expect(err).ToNot(HaveOccurred())

				_, err = tempFile.WriteString("cthulhu")
				Expect(err).ToNot(HaveOccurred())

				pluginPath = tempFile.Name()
			})

			AfterEach(func() {
				err := os.Remove(pluginPath)
				Expect(err).ToNot(HaveOccurred())
			})

			It("creates a copy of a file in plugin home and makes it executable", func() {
				copyPath, err := actor.CreateExecutableCopy(pluginPath)
				Expect(err).ToNot(HaveOccurred())

				contents, err := ioutil.ReadFile(copyPath)
				Expect(err).ToNot(HaveOccurred())
				Expect(contents).To(BeEquivalentTo("cthulhu"))

				stat, err := os.Stat(copyPath)
				Expect(err).ToNot(HaveOccurred())
				Expect(stat.Mode() & 0700).To(Equal(os.FileMode(0700)))
			})

			Context("when the plugin home does not exist", func() {
				BeforeEach(func() {
					fakeConfig.PluginHomeReturns("/a/file/that/does/not/exist")
				})

				It("returns an os.PathError", func() {
					_, err := actor.CreateExecutableCopy(pluginPath)
					_, isPathError := err.(*os.PathError)
					Expect(isPathError).To(BeTrue())
				})
			})
		})

		Context("when the file does not exist", func() {
			It("returns an os.PathError", func() {
				_, err := actor.CreateExecutableCopy("/a/file/that/does/not/exist")
				_, isPathError := err.(*os.PathError)
				Expect(isPathError).To(BeTrue())
			})
		})
	})

	Describe("FileExists", func() {
		var pluginPath string

		Context("when the file exists", func() {<commit_msg>remove test that fails when run as root on CI<commit_after>package pluginaction_test

import (
	"errors"
	"io/ioutil"
	"os"
	"path/filepath"

	. "code.cloudfoundry.org/cli/actor/pluginaction"
	"code.cloudfoundry.org/cli/actor/pluginaction/pluginactionfakes"
	"code.cloudfoundry.org/cli/util/configv3"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("install actions", func() {
	var (
		actor      Actor
		fakeConfig *pluginactionfakes.FakeConfig
		tempDir    string
	)

	BeforeEach(func() {
		fakeConfig = new(pluginactionfakes.FakeConfig)
		var err error
		tempDir, err = ioutil.TempDir("", "")
		Expect(err).ToNot(HaveOccurred())
		fakeConfig.PluginHomeReturns(tempDir)
		actor = NewActor(fakeConfig, nil)
	})

	AfterEach(func() {
		err := os.RemoveAll(tempDir)
		Expect(err).ToNot(HaveOccurred())
	})

	Describe("CreateExecutableCopy", func() {
		Context("when the file exists", func() {
			var pluginPath string

			BeforeEach(func() {
				tempFile, err := ioutil.TempFile("", "")
				Expect(err).ToNot(HaveOccurred())

				_, err = tempFile.WriteString("cthulhu")
				Expect(err).ToNot(HaveOccurred())

				pluginPath = tempFile.Name()
			})

			AfterEach(func() {
				err := os.Remove(pluginPath)
				Expect(err).ToNot(HaveOccurred())
			})

			It("creates a copy of a file in plugin home and makes it executable", func() {
				copyPath, err := actor.CreateExecutableCopy(pluginPath)
				Expect(err).ToNot(HaveOccurred())

				contents, err := ioutil.ReadFile(copyPath)
				Expect(err).ToNot(HaveOccurred())
				Expect(contents).To(BeEquivalentTo("cthulhu"))

				stat, err := os.Stat(copyPath)
				Expect(err).ToNot(HaveOccurred())
				Expect(stat.Mode() & 0700).To(Equal(os.FileMode(0700)))
			})
		})

		Context("when the file does not exist", func() {
			It("returns an os.PathError", func() {
				_, err := actor.CreateExecutableCopy("/a/file/that/does/not/exist")
				_, isPathError := err.(*os.PathError)
				Expect(isPathError).To(BeTrue())
			})
		})
	})

	Describe("FileExists", func() {
		var pluginPath string

		Context("when the file exists", func() {
{'fraction_non_alphanumeric': 0.1566238369517058, 'fraction_numerical': 0.003987594151528578, 'mean_word_length': 3.9889502762430937, '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': '7537783', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1567, 'n_words': 268}