Skip to content

Commit

Permalink
Merge pull request apache#16912 from [BEAM-13878] [Playground] Increa…
Browse files Browse the repository at this point in the history
…se test coverage for the fs_tool package

* Increase test coverage for the fs_tool package

* Rename folder

* Remove useless variable

* Update test names
  • Loading branch information
pavel-avilov authored and nancyxu123 committed Mar 9, 2022
1 parent a372e4e commit 50bb5c9
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 17 deletions.
63 changes: 51 additions & 12 deletions playground/backend/internal/fs_tool/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,29 @@ const (
destinationDir = "destinationDir"
testFileMode = 0755
pipelinesFolder = "executable_files"
fileName = "file.txt"
emptyFolder = "emptyFolder"
)

func TestMain(m *testing.M) {
err := prepareFiles()
if err != nil {
panic(fmt.Errorf("error during test setup: %s", err.Error()))
}
defer teardownFiles()
m.Run()
}

func prepareFiles() error {
err := os.Mkdir(sourceDir, testFileMode)
err := os.MkdirAll(filepath.Join(sourceDir, emptyFolder), testFileMode)
if err != nil {
return err
}
err = os.Mkdir(destinationDir, testFileMode)
if err != nil {
return err
}
filePath := filepath.Join(sourceDir, "file.txt")
filePath := filepath.Join(sourceDir, fileName)
_, err = os.Create(filePath)
return err
}
Expand All @@ -67,11 +78,6 @@ func teardownFolders(baseFileFolder string) error {
}

func TestLifeCycle_CopyFile(t *testing.T) {
if err := prepareFiles(); err != nil {
t.Fatalf("Error during preparing files for test: %s", err)
}
defer teardownFiles()

type fields struct {
folderGlobs []string
Paths LifeCyclePaths
Expand All @@ -88,7 +94,7 @@ func TestLifeCycle_CopyFile(t *testing.T) {
wantErr bool
}{
{
name: "file doesn't exist",
name: "File doesn't exist",
fields: fields{
folderGlobs: nil,
},
Expand All @@ -100,17 +106,29 @@ func TestLifeCycle_CopyFile(t *testing.T) {
wantErr: true,
},
{
name: "file exists",
name: "File exists",
fields: fields{
folderGlobs: nil,
},
args: args{
fileName: "file.txt",
fileName: fileName,
sourceDir: sourceDir,
destinationDir: destinationDir,
},
wantErr: false,
},
{
name: "Copy directory instead of file",
fields: fields{
folderGlobs: nil,
},
args: args{
fileName: emptyFolder,
sourceDir: sourceDir,
destinationDir: destinationDir,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -177,15 +195,15 @@ func TestLifeCycle_CreateSourceCodeFile(t *testing.T) {
wantErr bool
}{
{
name: "source file folder path doesn't exist",
name: "Source file folder path doesn't exist",
fields: fields{
Paths: LifeCyclePaths{
AbsoluteSourceFileFolderPath: "src",
},
}, wantErr: true,
},
{
name: "source file folder path exists",
name: "Source file folder path exists",
fields: fields{
Paths: LifeCyclePaths{
AbsoluteSourceFileFolderPath: filepath.Join(baseFileFolder, "src"),
Expand Down Expand Up @@ -343,6 +361,27 @@ func TestNewLifeCycle(t *testing.T) {
},
},
},
{
name: "SCIO LifeCycle",
args: args{
sdk: pb.Sdk_SDK_SCIO,
pipelineId: pipelineId,
pipelinesFolder: pipelinesFolder,
},
want: &LifeCycle{
folderGlobs: []string{baseFileFolder},
Paths: LifeCyclePaths{
SourceFileName: fmt.Sprintf("%s%s", pipelineId.String(), scioExecutableFileExtension),
AbsoluteSourceFileFolderPath: baseFileFolder,
AbsoluteSourceFilePath: filepath.Join(baseFileFolder, fmt.Sprintf("%s%s", pipelineId.String(), scioExecutableFileExtension)),
ExecutableFileName: fmt.Sprintf("%s%s", pipelineId.String(), scioExecutableFileExtension),
AbsoluteExecutableFileFolderPath: baseFileFolder,
AbsoluteExecutableFilePath: filepath.Join(baseFileFolder, fmt.Sprintf("%s%s", pipelineId.String(), scioExecutableFileExtension)),
AbsoluteBaseFolderPath: baseFileFolder,
AbsoluteLogFilePath: filepath.Join(baseFileFolder, logFileName),
},
},
},
{
name: "Unavailable SDK",
args: args{
Expand Down
2 changes: 1 addition & 1 deletion playground/backend/internal/fs_tool/go_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Test_newGoLifeCycle(t *testing.T) {
{
// Test case with calling newGoLifeCycle method with correct pipelineId and workingDir.
// As a result, want to receive an expected go life cycle.
name: "newGoLifeCycle",
name: "NewGoLifeCycle",
args: args{
pipelineId: pipelineId,
pipelinesFolder: filepath.Join(workingDir, pipelinesFolder),
Expand Down
17 changes: 14 additions & 3 deletions playground/backend/internal/fs_tool/java_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Test_newJavaLifeCycle(t *testing.T) {
{
// Test case with calling newJavaLifeCycle method with correct pipelineId and workingDir.
// As a result, want to receive an expected java life cycle.
name: "newJavaLifeCycle",
name: "NewJavaLifeCycle",
args: args{
pipelineId: pipelineId,
pipelinesFolder: filepath.Join(workingDir, pipelinesFolder),
Expand Down Expand Up @@ -96,10 +96,21 @@ func Test_executableName(t *testing.T) {
want string
wantErr bool
}{
{
// Test case with calling sourceFileName method with empty directory.
// As a result, want to receive an error.
name: "Directory is empty",
prepare: func() {},
args: args{
executableFolder: filepath.Join(workDir, pipelinesFolder, pipelineId.String(), "bin"),
},
want: "",
wantErr: true,
},
{
// Test case with calling sourceFileName method with correct pipelineId and workingDir.
// As a result, want to receive a name that should be executed
name: "get executable name",
name: "Get executable name",
prepare: func() {
compiled := filepath.Join(workDir, pipelinesFolder, pipelineId.String(), compiledFolderName)
filePath := filepath.Join(compiled, "temp.class")
Expand All @@ -117,7 +128,7 @@ func Test_executableName(t *testing.T) {
{
// Test case with calling sourceFileName method with wrong directory.
// As a result, want to receive an error.
name: "directory doesn't exist",
name: "Directory doesn't exist",
prepare: func() {},
args: args{
executableFolder: filepath.Join(workDir, pipelineId.String()),
Expand Down
93 changes: 93 additions & 0 deletions playground/backend/internal/fs_tool/path_checker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 fs_tool

import (
"fmt"
"path/filepath"
"testing"
)

func TestWrongExtension_Error(t *testing.T) {
errorMessage := "MOCK_ERROR"
type fields struct {
error string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Get error of the WrongExtension",
fields: fields{error: errorMessage},
want: fmt.Sprintf("File has wrong extension: %v", errorMessage),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &WrongExtension{
error: tt.fields.error,
}
if got := e.Error(); got != tt.want {
t.Errorf("Error() = %v, want %v", got, tt.want)
}
})
}
}

func TestCheckPathIsValid(t *testing.T) {
type args struct {
args []interface{}
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{
name: "File not exist",
args: args{args: []interface{}{"filePath.txt", ".txt"}},
want: false,
wantErr: true,
},
{
name: "Incorrect extension",
args: args{args: []interface{}{filepath.Join(sourceDir, fileName), JavaSourceFileExtension}},
want: false,
wantErr: true,
},
{
name: "CheckPathIsValid worked successfully",
args: args{args: []interface{}{filepath.Join(sourceDir, fileName), ".txt"}},
want: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CheckPathIsValid(tt.args.args...)
if (err != nil) != tt.wantErr {
t.Errorf("CheckPathIsValid() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("CheckPathIsValid() got = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion playground/backend/internal/fs_tool/python_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Test_newPythonLifeCycle(t *testing.T) {
{
// Test case with calling newPythonLifeCycle method with correct pipelineId and workingDir.
// As a result, want to receive an expected python life cycle.
name: "newPythonLifeCycle",
name: "NewPythonLifeCycle",
args: args{
pipelineId: pipelineId,
pipelinesFolder: filepath.Join(workingDir, pipelinesFolder),
Expand Down

0 comments on commit 50bb5c9

Please sign in to comment.