-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: unit tests for some of the new functions.
- Loading branch information
Showing
8 changed files
with
375 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package rollback | ||
|
||
import ( | ||
"fmt" | ||
"github.com/metrumresearchgroup/pkgr/testhelper" | ||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/suite" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
type OperationsTestSuite struct { | ||
suite.Suite | ||
FileSystem afero.Fs | ||
FilePrefix string | ||
} | ||
|
||
func (suite *OperationsTestSuite) SetupTest() { | ||
suite.FileSystem = afero.NewOsFs() | ||
suite.FilePrefix = "testsite/working" | ||
} | ||
|
||
func (suite *OperationsTestSuite) TearDownTest() { | ||
mm := afero.NewOsFs() | ||
mm.RemoveAll("testsite/working") | ||
} | ||
|
||
func TestOperationsTestSuite(t *testing.T) { | ||
suite.Run(t, new(OperationsTestSuite)) | ||
} | ||
|
||
func InitializeTestEnvironment(fileSystem afero.Fs, goldenSet string) { | ||
goldenSetPath := filepath.Join("testsite", "golden", goldenSet) | ||
testWorkDir := filepath.Join("testsite", "working") | ||
fileSystem.MkdirAll(testWorkDir, 0755) | ||
|
||
err := testhelper.CopyDir(fileSystem, goldenSetPath, testWorkDir) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func (suite *OperationsTestSuite) TestRollbackPackageEnvironment_DeletesOnlyNewPackages() { | ||
InitializeTestEnvironment(suite.FileSystem, "basic-test1") | ||
|
||
cwd, _ := filepath.Abs(".") | ||
fmt.Println(fmt.Sprintf("Starting test with working directory %s", cwd)) | ||
|
||
libraryPath, _ := filepath.Abs(filepath.Join("testsite", "working", "test-library")) | ||
|
||
rbpFixture := RollbackPlan{ | ||
NewPackages: []string {"R6"}, | ||
Library: libraryPath, | ||
} | ||
|
||
RollbackPackageEnvironment(suite.FileSystem, rbpFixture) | ||
|
||
suite.False(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "R6"))) | ||
suite.True(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "crayon"))) | ||
} | ||
|
||
func (suite *OperationsTestSuite) TestRollbackPackageEnvironment_DeletesMultiplePackages() { | ||
InitializeTestEnvironment(suite.FileSystem, "basic-test1") | ||
|
||
cwd, _ := filepath.Abs(".") | ||
fmt.Println(fmt.Sprintf("Starting test with working directory %s", cwd)) | ||
|
||
libraryPath, _ := filepath.Abs(filepath.Join("testsite", "working", "test-library")) | ||
|
||
rbpFixture := RollbackPlan{ | ||
NewPackages: []string {"R6", "crayon"}, | ||
Library: libraryPath, | ||
} | ||
|
||
RollbackPackageEnvironment(suite.FileSystem, rbpFixture) | ||
|
||
suite.False(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "R6"))) | ||
suite.False(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "crayon"))) | ||
} | ||
|
||
func (suite *OperationsTestSuite) TestRollbackPackageEnvironment_HandlesEmptyListOfPackages() { | ||
InitializeTestEnvironment(suite.FileSystem, "basic-test1") | ||
|
||
cwd, _ := filepath.Abs(".") | ||
fmt.Println(fmt.Sprintf("Starting test with working directory %s", cwd)) | ||
|
||
libraryPath, _ := filepath.Abs(filepath.Join(suite.FilePrefix, "basic-test1", "test-library")) | ||
|
||
rbpFixture := RollbackPlan{ | ||
NewPackages: []string {}, | ||
Library: libraryPath, | ||
} | ||
|
||
RollbackPackageEnvironment(suite.FileSystem, rbpFixture ) | ||
|
||
suite.True(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "R6"))) | ||
suite.True(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "crayon"))) | ||
} | ||
|
||
func (suite *OperationsTestSuite) TestRollbackPackageEnvironment_PackagesAreCaseSensitive() { | ||
InitializeTestEnvironment(suite.FileSystem, "basic-test1") | ||
|
||
cwd, _ := filepath.Abs(".") | ||
fmt.Println(fmt.Sprintf("Starting test with working directory %s", cwd)) | ||
|
||
libraryPath, _ := filepath.Abs(filepath.Join("testsite", "working", "test-library")) | ||
|
||
rbpFixture := RollbackPlan{ | ||
NewPackages: []string {"R6", "CRAYON"}, | ||
Library: libraryPath, | ||
} | ||
|
||
RollbackPackageEnvironment(suite.FileSystem, rbpFixture) | ||
|
||
suite.False(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "R6"))) | ||
suite.True(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "crayon"))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Version: 1 | ||
# top level packages | ||
Packages: | ||
- R6 | ||
- crayon | ||
|
||
# any repositories, order matters | ||
Repos: | ||
- CRAN: "https://cran.rstudio.com" | ||
|
||
Library: "test-library" |
24 changes: 24 additions & 0 deletions
24
rollback/testsite/golden/basic-test1/test-library/R6/DESCRIPTION
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Package: R6 | ||
Title: Encapsulated Classes with Reference Semantics | ||
Version: 2.4.0 | ||
Authors@R: person("Winston", "Chang", role = c("aut", "cre"), email = "winston@stdout.org") | ||
Description: Creates classes with reference semantics, similar to R's built-in | ||
reference classes. Compared to reference classes, R6 classes are simpler | ||
and lighter-weight, and they are not built on S4 classes so they do not | ||
require the methods package. These classes allow public and private | ||
members, and they support inheritance, even when the classes are defined in | ||
different packages. | ||
Depends: R (>= 3.0) | ||
Suggests: knitr, microbenchmark, pryr, testthat, ggplot2, scales | ||
License: MIT + file LICENSE | ||
URL: https://r6.r-lib.org, https://github.com/r-lib/R6/ | ||
LazyData: true | ||
BugReports: https://github.com/r-lib/R6/issues | ||
RoxygenNote: 6.1.1 | ||
NeedsCompilation: no | ||
Packaged: 2019-02-14 18:34:54 UTC; winston | ||
Author: Winston Chang [aut, cre] | ||
Maintainer: Winston Chang <winston@stdout.org> | ||
Repository: CRAN | ||
Date/Publication: 2019-02-14 19:42:14 UTC | ||
Built: R 3.5.2; ; 2019-02-15 15:07:41 UTC; unix |
35 changes: 35 additions & 0 deletions
35
rollback/testsite/golden/basic-test1/test-library/crayon/DESCRIPTION
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Package: crayon | ||
Title: Colored Terminal Output | ||
Version: 1.3.4 | ||
Authors@R: c( | ||
person("Gábor", "Csárdi", , "csardi.gabor@gmail.com", | ||
role = c("aut", "cre")), | ||
person( | ||
"Brodie", "Gaslam", email="brodie.gaslam@yahoo.com", | ||
role=c("ctb")) | ||
) | ||
Description: Colored terminal output on terminals that support 'ANSI' | ||
color and highlight codes. It also works in 'Emacs' 'ESS'. 'ANSI' | ||
color support is automatically detected. Colors and highlighting can | ||
be combined and nested. New styles can also be created easily. | ||
This package was inspired by the 'chalk' 'JavaScript' project. | ||
License: MIT + file LICENSE | ||
LazyData: true | ||
URL: https://github.com/r-lib/crayon#readme | ||
BugReports: https://github.com/r-lib/crayon/issues | ||
Collate: 'ansi-256.r' 'combine.r' 'string.r' 'utils.r' | ||
'crayon-package.r' 'disposable.r' 'has_ansi.r' 'has_color.r' | ||
'styles.r' 'machinery.r' 'parts.r' 'print.r' 'style-var.r' | ||
'show.r' 'string_operations.r' | ||
Imports: grDevices, methods, utils | ||
Suggests: mockery, rstudioapi, testthat, withr | ||
RoxygenNote: 6.0.1.9000 | ||
Encoding: UTF-8 | ||
NeedsCompilation: no | ||
Packaged: 2017-09-15 18:14:04 UTC; gaborcsardi | ||
Author: Gábor Csárdi [aut, cre], | ||
Brodie Gaslam [ctb] | ||
Maintainer: Gábor Csárdi <csardi.gabor@gmail.com> | ||
Repository: CRAN | ||
Date/Publication: 2017-09-16 19:49:46 UTC | ||
Built: R 3.5.0; ; 2018-04-23 04:01:40 UTC; unix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Version: 1 | ||
# top level packages | ||
Packages: | ||
- R6 | ||
- crayon | ||
|
||
# any repositories, order matters | ||
Repos: | ||
- CRAN: "https://cran.rstudio.com" | ||
|
||
Library: "test-library" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package rollback | ||
|
||
import ( | ||
"github.com/metrumresearchgroup/pkgr/desc" | ||
"github.com/stretchr/testify/suite" | ||
"testing" | ||
) | ||
|
||
type TypesTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestTypesTestSuite(t *testing.T) { | ||
suite.Run(t, new(TypesTestSuite)) | ||
} | ||
|
||
func (suite *TypesTestSuite) TestDiscernNewPackages_ToInstallCanOutnumberPreinstalled() { | ||
crayon := desc.Desc{ | ||
Package: "crayon", | ||
Version: "1.3.4", | ||
Repository: "CRAN", | ||
} | ||
|
||
r6 := desc.Desc{ | ||
Package: "R6", | ||
Version: "2.4.0", | ||
Repository: "CRAN", | ||
} | ||
|
||
toInstallFixture := []string{"R6", "crayon", "shiny"} | ||
|
||
preinstalledPackagesFixture := make(map[string]desc.Desc) | ||
preinstalledPackagesFixture["crayon"] = crayon | ||
preinstalledPackagesFixture["R6"] = r6 | ||
|
||
actual := DiscernNewPackages(toInstallFixture, preinstalledPackagesFixture) | ||
|
||
suite.Equal(1, len(actual)) | ||
suite.Equal("shiny", actual[0]) | ||
|
||
} | ||
|
||
func (suite *TypesTestSuite) TestDiscernNewPackages_AllPackagesPreinstalled() { | ||
crayon := desc.Desc{ | ||
Package: "crayon", | ||
Version: "1.3.4", | ||
Repository: "CRAN", | ||
} | ||
|
||
r6 := desc.Desc{ | ||
Package: "R6", | ||
Version: "2.4.0", | ||
Repository: "CRAN", | ||
} | ||
|
||
toInstallFixture := []string{"R6", "crayon"} | ||
|
||
preinstalledPackagesFixture := make(map[string]desc.Desc) | ||
preinstalledPackagesFixture["crayon"] = crayon | ||
preinstalledPackagesFixture["R6"] = r6 | ||
|
||
actual := DiscernNewPackages(toInstallFixture, preinstalledPackagesFixture) | ||
|
||
suite.Equal(0, len(actual)) | ||
} | ||
|
||
func (suite *TypesTestSuite) TestDiscernNewPackages_SomePackagesPreinstalled() { | ||
crayon := desc.Desc{ | ||
Package: "crayon", | ||
Version: "1.3.4", | ||
Repository: "CRAN", | ||
} | ||
|
||
toInstallFixture := []string{"R6", "crayon"} | ||
|
||
preinstalledPackagesFixture := make(map[string]desc.Desc) | ||
preinstalledPackagesFixture["crayon"] = crayon | ||
|
||
actual := DiscernNewPackages(toInstallFixture, preinstalledPackagesFixture) | ||
|
||
suite.Equal(1, len(actual)) | ||
suite.Equal("R6", actual[0]) | ||
} | ||
|
||
func (suite *TypesTestSuite) TestDiscernNewPackages_SomePackagesPreinstalled2() { | ||
crayon := desc.Desc{ | ||
Package: "crayon", | ||
Version: "1.3.4", | ||
Repository: "CRAN", | ||
} | ||
|
||
toInstallFixture := []string{"R6", "crayon", "RColorBrewer"} | ||
|
||
preinstalledPackagesFixture := make(map[string]desc.Desc) | ||
preinstalledPackagesFixture["crayon"] = crayon | ||
|
||
actual := DiscernNewPackages(toInstallFixture, preinstalledPackagesFixture) | ||
|
||
suite.Equal(2, len(actual)) | ||
suite.Equal("R6", actual[0]) | ||
suite.Equal("RColorBrewer", actual[1]) | ||
} | ||
|
||
func (suite *TypesTestSuite) TestDiscernNewPackages_PackagesAreCaseSensitive() { | ||
crayon := desc.Desc{ | ||
Package: "CRAYON", | ||
Version: "1.3.4", | ||
Repository: "CRAN", | ||
} | ||
|
||
r6 := desc.Desc{ | ||
Package: "R6", | ||
Version: "2.4.0", | ||
Repository: "CRAN", | ||
} | ||
|
||
toInstallFixture := []string{"R6", "crayon"} | ||
|
||
preinstalledPackagesFixture := make(map[string]desc.Desc) | ||
preinstalledPackagesFixture["CRAYON"] = crayon | ||
preinstalledPackagesFixture["R6"] = r6 | ||
|
||
actual := DiscernNewPackages(toInstallFixture, preinstalledPackagesFixture) | ||
|
||
suite.Equal(1, len(actual)) | ||
suite.Equal("crayon", actual[0]) //"crayon" is considered a new package because all we can see is "CRAYON" | ||
} |
Oops, something went wrong.