Skip to content

Commit

Permalink
Implement proper build options
Browse files Browse the repository at this point in the history
  • Loading branch information
adracus committed Jul 21, 2022
1 parent 8e37c63 commit 745225e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
22 changes: 19 additions & 3 deletions buildutils/buildutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ type ModMode string

const (
// ModModeVendor causes modules to be resolved from a vendor folder.
ModModeVendor = "vendor"
ModModeVendor ModMode = "vendor"
// ModModeReadonly expect all modules to be present in the module cache for the current module.
ModModeReadonly = "readonly"
ModModeReadonly ModMode = "readonly"
// ModModeMod fetches any module before building.
ModModeMod = "mod"
ModModeMod ModMode = "mod"
)

// ApplyToBuild implements BuildOption.
func (m ModMode) ApplyToBuild(o *BuildOptions) {
o.Mod = &m
}

// BuildOptions are options to supply for a Build.
type BuildOptions struct {
// ForceRebuild forces rebuilding of packages that are already up-to-date.
Expand Down Expand Up @@ -88,6 +93,17 @@ type BuildOption interface {
ApplyToBuild(o *BuildOptions)
}

// forceRebuild is an option to force rebuilding packages.
type forceRebuild struct{}

// ApplyToBuild implements BuildOption.
func (forceRebuild) ApplyToBuild(o *BuildOptions) {
o.ForceRebuild = true
}

// ForceRebuild is an option to force rebuilding packages.
var ForceRebuild = forceRebuild{}

// Build runs `go build` with the target output and name.
// If BuilderOptions.Tidy was set, it runs `go mod tidy` beforehand.
func (b *Builder) Build(name, filename string, opts ...BuildOption) error {
Expand Down
27 changes: 27 additions & 0 deletions buildutils/buildutils_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2022 OnMetal authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package buildutils_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestBuildutils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Buildutils Suite")
}
42 changes: 42 additions & 0 deletions buildutils/buildutils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022 OnMetal authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package buildutils_test

import (
. "github.com/onmetal/controller-utils/buildutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Buildutils", func() {
Describe("ModMode", func() {
It("should set the mod property of build options", func() {
mod := ModModeMod
o := &BuildOptions{}
mod.ApplyToBuild(o)

Expect(o.Mod).To(HaveValue(Equal(mod)))
})
})

Describe("ForceRebuild", func() {
It("should set the force rebuild property of build options", func() {
o := &BuildOptions{}
ForceRebuild.ApplyToBuild(o)

Expect(o.ForceRebuild).To(BeTrue())
})
})
})

0 comments on commit 745225e

Please sign in to comment.