Skip to content

Commit f4affd7

Browse files
committed
make independent of env vars
1 parent 68b661d commit f4affd7

File tree

6 files changed

+103
-54
lines changed

6 files changed

+103
-54
lines changed

Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
include ./Makefile.Common
22
include ./packaging/technical-addon/Makefile
3-
BUILD_DIR?=$(realpath .)
43
### VARIABLES
4+
SOURCE_DIR?=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
5+
BUILD_DIR?=$(git rev-parse --show-toplevel)
56

67
# BUILD_TYPE should be one of (dev, release).
78
BUILD_TYPE?=release
@@ -11,7 +12,7 @@ GIT_SHA=$(shell git rev-parse --short HEAD)
1112
GOARCH=$(shell go env GOARCH)
1213
GOOS=$(shell go env GOOS)
1314

14-
FIND_MOD_ARGS=-type f -name "go.mod"
15+
FIND_MOD_ARGS=-type f -name "go.mod" -not -path "./packaging/technical-addon/*"
1516
TO_MOD_DIR=dirname {} \; | sort | egrep '^./'
1617

1718
ALL_MODS := $(shell find . $(FIND_MOD_ARGS) -exec $(TO_MOD_DIR)) $(PWD)

Makefile.Common

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Explicitly define the shell we will use for commands.
22
SHELL?=/bin/bash
3+
SOURCE_DIR?=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
34
BUILD_DIR?=$(git rev-parse --show-toplevel)
45

56
# Add support to use Makefile on Windows

packaging/technical-addon/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Used for building
2-
SOURCE_DIR:=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
3-
BUILD_DIR?=$(realpath $(SOURCE_DIR)/../../)/build
2+
SOURCE_DIR=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
3+
BUILD_DIR=$(realpath $(SOURCE_DIR)/../../)/build
44
COMMON_MAKEFILE:=$(abspath $(SOURCE_DIR)/../../Makefile.Common)
55

66
ABS_MAKEFILE_LIST := $(foreach file,$(MAKEFILE_LIST),$(abspath $(file)))
@@ -39,7 +39,7 @@ test-ta-build-tools: ta-build-tools
3939
mkdir -p $(BUILD_DIR)/Sample_Addon/$(PLATFORM)_$(SPLUNK_ARCH)/bin; \
4040
cd $(SOURCE_DIR) && go build $(GO_BUILD_FLAGS) -o $(BUILD_DIR)/Sample_Addon/$(PLATFORM)_$(SPLUNK_ARCH)/bin/Sample_Addon ./cmd/modinput_config_generator/internal/testdata/pkg/sample_addon/runner || exit 1; \
4141
echo "built addon, testing..."
42-
cd $(SOURCE_DIR) && SOURCE_DIR="$(SOURCE_DIR)/cmd/modinput_config_generator/internal/testdata" BUILD_DIR="$(BUILD_DIR)" go test -v ./...
42+
cd $(SOURCE_DIR) && go test -v ./...
4343

4444
.PHONY: generate-technical-addon
4545
generate-technical-addon: env-guard-all

packaging/technical-addon/cmd/modinput_config_generator/generator_test.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import (
2929
"github.com/docker/docker/api/types/container"
3030
"github.com/docker/docker/api/types/mount"
3131
"github.com/google/go-cmp/cmp"
32-
"github.com/splunk/splunk-technical-addon/internal/modularinput"
3332
"github.com/splunk/splunk-technical-addon/internal/packaging"
33+
"github.com/splunk/splunk-technical-addon/internal/testcommon"
3434
"github.com/stretchr/testify/assert"
3535
"github.com/stretchr/testify/require"
3636
"github.com/testcontainers/testcontainers-go"
@@ -78,7 +78,7 @@ func TestRunner(t *testing.T) {
7878
ctx := context.Background()
7979
addonPath := filepath.Join(t.TempDir(), "Sample_Addon.tgz")
8080

81-
buildDir := modularinput.GetBuildDir()
81+
buildDir := testcommon.GetBuildDir()
8282
require.NotEmpty(t, buildDir)
8383
err := packaging.PackageAddon(filepath.Join(buildDir, "Sample_Addon"), addonPath)
8484
require.NoError(t, err)
@@ -110,7 +110,9 @@ func TestRunner(t *testing.T) {
110110
}
111111

112112
func TestRunnerConfigGeneration(t *testing.T) {
113-
113+
sourceDir, err := testcommon.GetSourceDir()
114+
require.NoError(t, err)
115+
sourceDir = filepath.Join(sourceDir, "cmd", "modinput_config_generator", "internal", "testdata")
114116
tests := []struct {
115117
testSchemaName string
116118
sampleYamlPath string
@@ -120,7 +122,7 @@ func TestRunnerConfigGeneration(t *testing.T) {
120122
{
121123
testSchemaName: "Sample_Addon",
122124
outDir: t.TempDir(),
123-
sampleYamlPath: filepath.Join(os.Getenv("SOURCE_DIR"), "pkg/sample_addon/runner/modular-inputs.yaml"),
125+
sampleYamlPath: filepath.Join(sourceDir, "pkg/sample_addon/runner/modular-inputs.yaml"),
124126
},
125127
}
126128

@@ -136,7 +138,9 @@ func TestRunnerConfigGeneration(t *testing.T) {
136138
}
137139

138140
func TestInputsConfGeneration(t *testing.T) {
139-
141+
sourceDir, err := testcommon.GetSourceDir()
142+
require.NoError(t, err)
143+
sourceDir = filepath.Join(sourceDir, "cmd", "modinput_config_generator", "internal", "testdata")
140144
tests := []struct {
141145
testSchemaName string
142146
sampleYamlPath string
@@ -148,8 +152,8 @@ func TestInputsConfGeneration(t *testing.T) {
148152
{
149153
testSchemaName: "Sample_Addon",
150154
outDir: t.TempDir(),
151-
sourceDir: filepath.Join(os.Getenv("SOURCE_DIR"), "pkg/sample_addon"),
152-
sampleYamlPath: filepath.Join(os.Getenv("SOURCE_DIR"), "pkg/sample_addon/runner/modular-inputs.yaml"),
155+
sourceDir: filepath.Join(sourceDir, "pkg/sample_addon"),
156+
sampleYamlPath: filepath.Join(sourceDir, "pkg/sample_addon/runner/modular-inputs.yaml"),
153157
},
154158
}
155159

packaging/technical-addon/internal/modularinput/inputs_processor.go

-42
Original file line numberDiff line numberDiff line change
@@ -143,45 +143,3 @@ func (mit *ModinputProcessor) GetMissingRequired(provided map[string]bool) []str
143143
}
144144
return missing
145145
}
146-
147-
func GetBuildDir() string {
148-
var err error
149-
buildDir := os.Getenv("BUILD_DIR")
150-
if buildDir == "" {
151-
fmt.Println("$BUILD_DIR not set, searching for git root")
152-
buildDir, err = findGitRoot(".")
153-
if err != nil || buildDir == "" {
154-
fmt.Println("no git dir found, defaulting to cwd")
155-
buildDir, err = os.Getwd()
156-
if err != nil {
157-
fmt.Println("couldn't get cwd, defaulting to ./")
158-
buildDir = "."
159-
}
160-
}
161-
buildDir = filepath.Join(buildDir, "build")
162-
}
163-
return buildDir
164-
}
165-
166-
func findGitRoot(startDir string) (string, error) {
167-
dir, err := filepath.Abs(startDir)
168-
if err != nil {
169-
return "", fmt.Errorf("failed to get absolute path: %w", err)
170-
}
171-
172-
for {
173-
gitPath := filepath.Join(dir, ".git")
174-
_, err := os.Stat(gitPath)
175-
if err == nil {
176-
return dir, nil
177-
} else if !os.IsNotExist(err) {
178-
return "", fmt.Errorf("error checking for .git: %w", err)
179-
}
180-
181-
parentDir := filepath.Dir(dir)
182-
if parentDir == dir {
183-
return "", fmt.Errorf("no git repository found")
184-
}
185-
dir = parentDir
186-
}
187-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package testcommon
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
func GetBuildDir() string {
10+
var err error
11+
buildDir := os.Getenv("BUILD_DIR")
12+
if buildDir == "" {
13+
fmt.Println("$BUILD_DIR not set, searching for git root")
14+
buildDir, err = findGitRoot(".")
15+
if err != nil || buildDir == "" {
16+
fmt.Println("no git dir found, defaulting to cwd")
17+
buildDir, err = os.Getwd()
18+
if err != nil {
19+
fmt.Println("couldn't get cwd, defaulting to ./")
20+
buildDir = "."
21+
}
22+
}
23+
buildDir = filepath.Join(buildDir, "build")
24+
}
25+
return buildDir
26+
}
27+
28+
func findGitRoot(startDir string) (string, error) {
29+
dir, err := filepath.Abs(startDir)
30+
if err != nil {
31+
return "", fmt.Errorf("failed to get absolute path: %w", err)
32+
}
33+
34+
for {
35+
gitPath := filepath.Join(dir, ".git")
36+
_, err := os.Stat(gitPath)
37+
if err == nil {
38+
return dir, nil
39+
} else if !os.IsNotExist(err) {
40+
return "", fmt.Errorf("error checking for .git: %w", err)
41+
}
42+
43+
parentDir := filepath.Dir(dir)
44+
if parentDir == dir {
45+
return "", fmt.Errorf("no git repository found")
46+
}
47+
dir = parentDir
48+
}
49+
}
50+
51+
func GetSourceDir() (string, error) {
52+
var err error
53+
sourceDir := os.Getenv("SOURCE_DIR")
54+
if sourceDir == "" {
55+
fmt.Println("SOURCE_DIR not set, searching for make root")
56+
sourceDir, err = findSourceRoot(".")
57+
if err != nil || sourceDir == "" {
58+
return "", fmt.Errorf("could not makefile to use as SOURCE_DIR and SOURCE_DIR was not specified as an env var")
59+
}
60+
}
61+
return sourceDir, nil
62+
}
63+
64+
func findSourceRoot(startDir string) (string, error) {
65+
dir, err := filepath.Abs(startDir)
66+
if err != nil {
67+
return "", fmt.Errorf("failed to get absolute path: %w", err)
68+
}
69+
70+
for {
71+
gitPath := filepath.Join(dir, "Makefile")
72+
_, err := os.Stat(gitPath)
73+
if err == nil {
74+
return dir, nil
75+
} else if !os.IsNotExist(err) {
76+
return "", fmt.Errorf("error checking for Makefile: %w", err)
77+
}
78+
79+
parentDir := filepath.Dir(dir)
80+
if parentDir == dir {
81+
return "", fmt.Errorf("no Makefile found")
82+
}
83+
dir = parentDir
84+
}
85+
}

0 commit comments

Comments
 (0)