Skip to content

Commit

Permalink
Add Buck Build + Test Capabilities (#404)
Browse files Browse the repository at this point in the history
Summary:

Add these capabilities

* Build the ttpforge binary using internal golang dependencies and tooling
* Run unit tests using buck

Reviewed By: l50

Differential Revision: D50708311
  • Loading branch information
d3sch41n authored and facebook-github-bot committed Oct 30, 2023
1 parent ad00b0d commit 0c9cdc0
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 553 deletions.
5 changes: 2 additions & 3 deletions cmd/listttps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package cmd_test
package cmd

import (
"path/filepath"
"testing"

"github.com/facebookincubator/ttpforge/cmd"
"github.com/stretchr/testify/require"
)

Expand All @@ -41,7 +40,7 @@ func TestListTTPs(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
rc := cmd.BuildRootCommand(nil)
rc := BuildRootCommand(nil)
rc.SetArgs([]string{"list", "ttps", "-c", testConfigFilePath})
err := rc.Execute()
if tc.wantError {
Expand Down
6 changes: 3 additions & 3 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package cmd_test
package cmd

import (
"bytes"
"path/filepath"

"testing"

"github.com/facebookincubator/ttpforge/cmd"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestRun(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var stdoutBuf, stderrBuf bytes.Buffer
rc := cmd.BuildRootCommand(&cmd.Config{
rc := BuildRootCommand(&Config{
Stdout: &stdoutBuf,
Stderr: &stderrBuf,
})
Expand Down
25 changes: 12 additions & 13 deletions pkg/args/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package args_test
package args

import (
"testing"

"github.com/facebookincubator/ttpforge/pkg/args"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -31,14 +30,14 @@ func TestValidateArgs(t *testing.T) {

testCases := []struct {
name string
specs []args.Spec
specs []Spec
argKvStrs []string
expectedResult map[string]any
wantError bool
}{
{
name: "Parse String and Integer Arguments",
specs: []args.Spec{
specs: []Spec{
{
Name: "alpha",
},
Expand All @@ -59,7 +58,7 @@ func TestValidateArgs(t *testing.T) {
},
{
name: "Parse String and Integer Argument (Default Value)",
specs: []args.Spec{
specs: []Spec{
{
Name: "alpha",
},
Expand All @@ -80,7 +79,7 @@ func TestValidateArgs(t *testing.T) {
},
{
name: "Handle Extra Equals",
specs: []args.Spec{
specs: []Spec{
{
Name: "alpha",
},
Expand All @@ -100,7 +99,7 @@ func TestValidateArgs(t *testing.T) {
},
{
name: "Invalid Inputs (no '=')",
specs: []args.Spec{
specs: []Spec{
{
Name: "alpha",
},
Expand All @@ -116,7 +115,7 @@ func TestValidateArgs(t *testing.T) {
},
{
name: "Invalid Inputs (Missing Required Argument)",
specs: []args.Spec{
specs: []Spec{
{
Name: "alpha",
},
Expand All @@ -131,7 +130,7 @@ func TestValidateArgs(t *testing.T) {
},
{
name: "Argument Name Not In Specs",
specs: []args.Spec{
specs: []Spec{
{
Name: "alpha",
},
Expand All @@ -147,7 +146,7 @@ func TestValidateArgs(t *testing.T) {
},
{
name: "Duplicate Name in Specs",
specs: []args.Spec{
specs: []Spec{
{
Name: "alpha",
},
Expand All @@ -162,7 +161,7 @@ func TestValidateArgs(t *testing.T) {
},
{
name: "Wrong Type (string instead of int)",
specs: []args.Spec{
specs: []Spec{
{
Name: "alpha",
},
Expand All @@ -179,7 +178,7 @@ func TestValidateArgs(t *testing.T) {
},
{
name: "Default Value Wrong Type (string instead of int)",
specs: []args.Spec{
specs: []Spec{
{
Name: "alpha",
Type: "int",
Expand All @@ -195,7 +194,7 @@ func TestValidateArgs(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
args, err := args.ParseAndValidate(tc.specs, tc.argKvStrs)
args, err := ParseAndValidate(tc.specs, tc.argKvStrs)
if tc.wantError {
require.Error(t, err)
return
Expand Down
12 changes: 5 additions & 7 deletions pkg/blocks/basicstep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package blocks_test
package blocks

import (
"testing"

"github.com/facebookincubator/ttpforge/pkg/blocks"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -65,7 +63,7 @@ steps:

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var ttps blocks.TTP
var ttps TTP
err := yaml.Unmarshal([]byte(tc.content), &ttps)
if tc.wantError {
assert.Error(t, err)
Expand All @@ -85,9 +83,9 @@ outputs:
first:
filters:
- json_path: foo.bar`
var s blocks.BasicStep
execCtx := blocks.TTPExecutionContext{
Cfg: blocks.TTPExecutionConfig{
var s BasicStep
execCtx := TTPExecutionContext{
Cfg: TTPExecutionConfig{
Args: map[string]any{
"myarg": "baz",
},
Expand Down
9 changes: 4 additions & 5 deletions pkg/blocks/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package blocks_test
package blocks

import (
"io/fs"
Expand All @@ -27,7 +27,6 @@ import (
"strings"
"testing"

"github.com/facebookincubator/ttpforge/pkg/blocks"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -78,7 +77,7 @@ func TestFetchAbs(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := blocks.FetchAbs(tc.inputPath, tc.inputWorkdir)
result, err := FetchAbs(tc.inputPath, tc.inputWorkdir)

if tc.expectError {
assert.Error(t, err)
Expand Down Expand Up @@ -161,7 +160,7 @@ func TestFindFilePath(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := blocks.FindFilePath(tc.inputPath, tc.inputWorkdir, tc.fsStat)
result, err := FindFilePath(tc.inputPath, tc.inputWorkdir, tc.fsStat)

if tc.expectError {
assert.Error(t, err)
Expand Down Expand Up @@ -213,7 +212,7 @@ func TestFetchEnv(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := blocks.FetchEnv(tt.environ)
result := FetchEnv(tt.environ)
sort.Strings(result)
sort.Strings(tt.expected)

Expand Down
21 changes: 10 additions & 11 deletions pkg/blocks/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,30 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package blocks_test
package blocks

import (
"testing"

"github.com/facebookincubator/ttpforge/pkg/blocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestExpandVariablesStepResults(t *testing.T) {
// build the test fixture used across all cases
stepResults := blocks.NewStepResultsRecord()
stepResults.ByName["first_step"] = &blocks.ExecutionResult{
ActResult: blocks.ActResult{
stepResults := NewStepResultsRecord()
stepResults.ByName["first_step"] = &ExecutionResult{
ActResult: ActResult{
Stdout: "hello",
},
}
stepResults.ByName["second_step"] = &blocks.ExecutionResult{
ActResult: blocks.ActResult{
stepResults.ByName["second_step"] = &ExecutionResult{
ActResult: ActResult{
Stdout: "world",
},
}
stepResults.ByName["third_step"] = &blocks.ExecutionResult{
ActResult: blocks.ActResult{
stepResults.ByName["third_step"] = &ExecutionResult{
ActResult: ActResult{
Stdout: `{"foo":{"bar":"baz"}}`,
Outputs: map[string]string{
"myresult": "baz",
Expand All @@ -51,8 +50,8 @@ func TestExpandVariablesStepResults(t *testing.T) {
stepResults.ByIndex = append(stepResults.ByIndex, stepResults.ByName["first_step"])
stepResults.ByIndex = append(stepResults.ByIndex, stepResults.ByName["second_step"])
stepResults.ByIndex = append(stepResults.ByIndex, stepResults.ByName["third_step"])
execCtx := blocks.TTPExecutionContext{
Cfg: blocks.TTPExecutionConfig{
execCtx := TTPExecutionContext{
Cfg: TTPExecutionConfig{
Args: map[string]any{
"arg1": "myarg1",
"arg2": "myarg2",
Expand Down
17 changes: 8 additions & 9 deletions pkg/blocks/createfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package blocks_test
package blocks

import (
"os"
"testing"

"github.com/facebookincubator/ttpforge/pkg/blocks"
"github.com/facebookincubator/ttpforge/pkg/testutils"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
Expand All @@ -34,30 +33,30 @@ func TestCreateFileExecute(t *testing.T) {
testCases := []struct {
name string
description string
step *blocks.CreateFileStep
step *CreateFileStep
fsysContents map[string][]byte
expectExecuteError bool
}{
{
name: "Create Valid File",
description: "Create a single unremarkable file",
step: &blocks.CreateFileStep{
step: &CreateFileStep{
Path: "valid-file.txt",
Contents: "hello world",
},
},
{
name: "Nested Directories",
description: "Afero should handle this under the hood",
step: &blocks.CreateFileStep{
step: &CreateFileStep{
Path: "/directory/does/not/exist",
Contents: "should still work",
},
},
{
name: "Already Exists (No Overwrite)",
description: "Should fail because file already exists",
step: &blocks.CreateFileStep{
step: &CreateFileStep{
Path: "already-exists.txt",
Contents: "will fail",
},
Expand All @@ -69,7 +68,7 @@ func TestCreateFileExecute(t *testing.T) {
{
name: "Already Exists (With Overwrite)",
description: "Should succeed and overwrite existing file",
step: &blocks.CreateFileStep{
step: &CreateFileStep{
Path: "already-exists.txt",
Contents: "will succeed",
Overwrite: true,
Expand All @@ -81,7 +80,7 @@ func TestCreateFileExecute(t *testing.T) {
{
name: "Set Permissions Manually",
description: "Make the file read-only",
step: &blocks.CreateFileStep{
step: &CreateFileStep{
Path: "make-read-only",
Contents: "very-read-only",
Mode: 0400,
Expand All @@ -101,7 +100,7 @@ func TestCreateFileExecute(t *testing.T) {
}

// execute and check error
var execCtx blocks.TTPExecutionContext
var execCtx TTPExecutionContext
_, err := tc.step.Execute(execCtx)
if tc.expectExecuteError {
require.Error(t, err)
Expand Down
Loading

0 comments on commit 0c9cdc0

Please sign in to comment.