Skip to content

Commit

Permalink
Custom TMP_DIR doesn't add nr-integrations subfolder
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarocabanas committed Feb 13, 2024
1 parent 00a9a9d commit 508f237
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
13 changes: 5 additions & 8 deletions persist/store_path_test.go
@@ -1,12 +1,13 @@
package persist

import (
"github.com/newrelic/infra-integrations-sdk/log"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
"time"

"github.com/newrelic/infra-integrations-sdk/log"
"github.com/stretchr/testify/assert"
)

var tmpDir string
Expand Down Expand Up @@ -85,15 +86,11 @@ func TestStorePath_GetFilePath(t *testing.T) {
}{
{
tempDir: "",
expected: filepath.Join(tmpIntegrationDir(""), "com.newrelic.fake-c.json"),
},
{
tempDir: "",
expected: filepath.Join(tmpIntegrationDir(os.TempDir()), "com.newrelic.fake-c.json"),
expected: filepath.Join(os.TempDir(), integrationsDir, "com.newrelic.fake-c.json"),
},
{
tempDir: "custom-tmp",
expected: filepath.Join(tmpIntegrationDir("custom-tmp"), "com.newrelic.fake-c.json"),
expected: filepath.Join("custom-tmp", "com.newrelic.fake-c.json"),
},
}

Expand Down
5 changes: 4 additions & 1 deletion persist/storer.go
Expand Up @@ -92,12 +92,15 @@ func TmpPath(tempDir, integrationName string) string {
return file
}

// if tempDir is set the integrations tempDir will be the one provided, if not, it will use the OS tempDir with
// "nr-integrations" as a subfolder, Linux example: "/tmp/nr-integrations"
func tmpIntegrationDir(tempDir string) string {
dir := tempDir
if tempDir == "" {
tempDir = os.TempDir()
dir = filepath.Join(tempDir, integrationsDir)
}

dir := filepath.Join(tempDir, integrationsDir)
// Create integrations Storer directory
if os.MkdirAll(dir, dirFilePerm) != nil {
dir = tempDir
Expand Down
4 changes: 2 additions & 2 deletions persist/storer_test.go
Expand Up @@ -524,8 +524,8 @@ func TestFileStoreTmpPath_Save_and_Delete(t *testing.T) {
tempDir := path.Join(t.TempDir(), "custom")

// Given a file storer
// filePath includes integrationsDir conetant because the call to TmpPath sets that subFolder.
filePath := path.Join(tempDir, integrationsDir, "test.json")
// filePath should not include integrationsDir subFolder but the overriden tempDir
filePath := path.Join(tempDir, "test.json")
ttl := 1 * time.Second

storer, err := NewFileStore(TmpPath(tempDir, "test"), log.NewStdErr(true), ttl)
Expand Down

0 comments on commit 508f237

Please sign in to comment.