Skip to content

Commit

Permalink
[#200] add vcs.factory package
Browse files Browse the repository at this point in the history
  • Loading branch information
mengdaming committed Feb 1, 2023
1 parent 3bc8b97 commit d279937
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/engine/tcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ SOFTWARE.
package engine

import (
"fmt"
"github.com/murex/tcr/checker"
"github.com/murex/tcr/events"
"github.com/murex/tcr/filesystem"
Expand All @@ -39,8 +38,7 @@ import (
"github.com/murex/tcr/toolchain"
"github.com/murex/tcr/ui"
"github.com/murex/tcr/vcs"
"github.com/murex/tcr/vcs/git"
"github.com/murex/tcr/vcs/p4"
"github.com/murex/tcr/vcs/factory"
"gopkg.in/tomb.v2"
"os"
"strings"
Expand Down Expand Up @@ -249,18 +247,14 @@ func (tcr *TCREngine) initVCS(vcsName string) {
if tcr.vcs != nil {
return // VCS should be initialized only once
}

var err error
switch strings.ToLower(vcsName) {
case git.Name:
tcr.vcs, err = git.New(tcr.sourceTree.GetBaseDir())
case p4.Name:
tcr.vcs, err = p4.New(tcr.sourceTree.GetBaseDir())
tcr.vcs, err = factory.InitVCS(vcsName, tcr.sourceTree.GetBaseDir())
switch err.(type) {
case *factory.UnsupportedVCSError:
tcr.handleError(err, true, status.ConfigError)
default:
tcr.handleError(fmt.Errorf("VCS not supported: %s", vcsName), true, status.ConfigError)
return
tcr.handleError(err, true, status.VCSError)
}
tcr.handleError(err, true, status.VCSError)
}

func (tcr *TCREngine) initSourceTree(p params.Params) {
Expand Down
53 changes: 53 additions & 0 deletions src/vcs/factory/vcs_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright (c) 2023 Murex
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package factory

import (
"fmt"
"github.com/murex/tcr/vcs"
"github.com/murex/tcr/vcs/git"
"github.com/murex/tcr/vcs/p4"
"strings"
)

// UnsupportedVCSError is returned when the provided VCS name is not supported by the factory.
type UnsupportedVCSError struct {
vcsName string
}

// Error returns the error description
func (e *UnsupportedVCSError) Error() string {
return fmt.Sprintf("VCS not supported: \"%s\"", e.vcsName)
}

// InitVCS returns the VCS instance of type defined by name, working on the provided directory
func InitVCS(name string, dir string) (vcs.Interface, error) {
switch strings.ToLower(name) {
case git.Name:
return git.New(dir)
case p4.Name:
return p4.New(dir)
default:
return nil, &UnsupportedVCSError{name}
}
}
50 changes: 50 additions & 0 deletions src/vcs/factory/vcs_factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright (c) 2023 Murex
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package factory

import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)

func Test_vcs_factory_returns_an_error_when_vcs_is_not_supported(t *testing.T) {
v, err := InitVCS("unknown-vcs", "")
assert.IsType(t, &UnsupportedVCSError{}, err)
assert.Zero(t, v)
}

func Test_unsupported_vcs_message_format(t *testing.T) {
err := UnsupportedVCSError{"some-vcs"}
assert.Equal(t, "VCS not supported: \"some-vcs\"", err.Error())
}

func Test_vcs_factory_supports_git(t *testing.T) {
_, err := InitVCS("git", "")
assert.NotEqual(t, reflect.TypeOf(&UnsupportedVCSError{}), reflect.TypeOf(err))
}

func Test_vcs_factory_supports_p4(t *testing.T) {
_, err := InitVCS("p4", "")
assert.NotEqual(t, reflect.TypeOf(&UnsupportedVCSError{}), reflect.TypeOf(err))
}

0 comments on commit d279937

Please sign in to comment.