diff --git a/.gitignore b/.gitignore index 1521c8b..c23abfe 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ dist +__pycache__ +vendor \ No newline at end of file diff --git a/Makefile b/Makefile index 5164ce0..57d0b65 100644 --- a/Makefile +++ b/Makefile @@ -15,8 +15,7 @@ test: test_with_git: build CODECRAFTERS_SUBMISSION_DIR=$(shell pwd)/internal/test_helpers/pass_all \ - CODECRAFTERS_CURRENT_STAGE_SLUG="clone_repository" \ - CODECRAFTERS_COURSE_PAGE_URL="test" \ + CODECRAFTERS_TEST_CASES_JSON="[{"slug":"init","tester_log_prefix":"stage-1","title":"Stage #1: Initialize the .git directory"},{"slug":"read_blob","tester_log_prefix":"stage-2","title":"Stage #2: Read a blob object"},{"slug":"create_blob","tester_log_prefix":"stage-3","title":"Stage #3: Create a blob object"},{"slug":"read_tree","tester_log_prefix":"stage-4","title":"Stage #4: Read a tree object"},{"slug":"write_tree","tester_log_prefix":"stage-5","title":"Stage #5: Write a tree object"},{"slug":"create_commit","tester_log_prefix":"stage-6","title":"Stage #6: Create a commit"},{"slug":"clone_repository","tester_log_prefix":"stage-7","title":"Stage #7: Clone a repository"}]" \ dist/main.out copy_course_file: diff --git a/README.md b/README.md index 0aab418..c1bd517 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This is a program that validates your progress on the Git challenge. - Following environment variables: - `CODECRAFTERS_SUBMISSION_DIR` - root of the user's code submission - - `CODECRAFTERS_CURRENT_STAGE_SLUG` - current stage the user is on + - `CODECRAFTERS_TEST_CASES_JSON` - test cases in JSON format # User code requirements diff --git a/go.mod b/go.mod index 7a6b781..fb5b3e0 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,32 @@ module github.com/codecrafters-io/git-tester -go 1.14 +go 1.20 require ( - github.com/codecrafters-io/tester-utils v0.1.46 - github.com/fatih/color v1.13.0 // indirect + github.com/codecrafters-io/tester-utils v0.2.0 github.com/go-git/go-billy/v5 v5.0.0 github.com/go-git/go-git/v5 v5.0.0 - github.com/mattn/go-colorable v0.1.12 // indirect - github.com/stretchr/testify v1.7.0 - golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/emirpasic/gods v1.12.0 // indirect + github.com/fatih/color v1.15.0 // indirect + github.com/go-git/gcfg v1.5.0 // indirect + github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect + github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/mitchellh/go-testing-interface v1.14.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/sergi/go-diff v1.1.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect + github.com/xanzy/ssh-agent v0.2.1 // indirect + golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 // indirect + golang.org/x/net v0.0.0-20200301022130-244492dfa37a // indirect + golang.org/x/sys v0.12.0 // indirect + gopkg.in/warnings.v0 v0.1.2 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index a546dd6..92bc1ef 100644 --- a/go.sum +++ b/go.sum @@ -4,18 +4,16 @@ github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= -github.com/codecrafters-io/tester-utils v0.1.46 h1:kchXzxm6FLrR06Ab1/0iaOi9JiHzosCTzcg6vlw4yXg= -github.com/codecrafters-io/tester-utils v0.1.46/go.mod h1:VifsWEvXrRB18l2+rkE7gwuzWRhGX4AffJ01VTgMYfw= +github.com/codecrafters-io/tester-utils v0.2.0 h1:LHGrqE5wVaOoJ7aSa5vS93W3ToR7/tnHt3ATIt9z8gE= +github.com/codecrafters-io/tester-utils v0.2.0/go.mod h1:BeSLyqBpFxUwIm41QlnuRG7ZsabBXWE2Ga3LMFUXAPM= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg= github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= -github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= -github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= -github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= -github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ= +github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= +github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0= github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= @@ -39,13 +37,11 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= -github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= -github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU= @@ -60,8 +56,8 @@ github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= @@ -74,13 +70,11 @@ golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c= -golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= @@ -95,6 +89,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/assertions.go b/internal/assertions.go index b7b021d..a889122 100644 --- a/internal/assertions.go +++ b/internal/assertions.go @@ -4,7 +4,7 @@ import ( "fmt" "strings" - tester_utils "github.com/codecrafters-io/tester-utils" + executable "github.com/codecrafters-io/tester-utils/executable" ) func assertEqual(actual string, expected string) error { @@ -15,7 +15,7 @@ func assertEqual(actual string, expected string) error { return nil } -func assertStdout(result tester_utils.ExecutableResult, expected string) error { +func assertStdout(result executable.ExecutableResult, expected string) error { actual := string(result.Stdout) if expected != actual { return fmt.Errorf("Expected %q as stdout, got: %q", expected, actual) @@ -24,7 +24,7 @@ func assertStdout(result tester_utils.ExecutableResult, expected string) error { return nil } -func assertStderr(result tester_utils.ExecutableResult, expected string) error { +func assertStderr(result executable.ExecutableResult, expected string) error { actual := string(result.Stderr) if expected != actual { return fmt.Errorf("Expected %q as stderr, got: %q", expected, actual) @@ -33,7 +33,7 @@ func assertStderr(result tester_utils.ExecutableResult, expected string) error { return nil } -func assertStdoutContains(result tester_utils.ExecutableResult, expectedSubstring string) error { +func assertStdoutContains(result executable.ExecutableResult, expectedSubstring string) error { actual := string(result.Stdout) if !strings.Contains(actual, expectedSubstring) { return fmt.Errorf("Expected stdout to contain %q, got: %q", expectedSubstring, actual) @@ -42,7 +42,7 @@ func assertStdoutContains(result tester_utils.ExecutableResult, expectedSubstrin return nil } -func assertStderrContains(result tester_utils.ExecutableResult, expectedSubstring string) error { +func assertStderrContains(result executable.ExecutableResult, expectedSubstring string) error { actual := string(result.Stderr) if !strings.Contains(actual, expectedSubstring) { return fmt.Errorf("Expected stderr to contain %q, got: %q", expectedSubstring, actual) @@ -51,7 +51,7 @@ func assertStderrContains(result tester_utils.ExecutableResult, expectedSubstrin return nil } -func assertExitCode(result tester_utils.ExecutableResult, expected int) error { +func assertExitCode(result executable.ExecutableResult, expected int) error { actual := result.ExitCode if expected != actual { return fmt.Errorf("Expected %d as exit code, got: %d", expected, actual) diff --git a/internal/stage_clone_repository.go b/internal/stage_clone_repository.go index 89a390d..618017f 100644 --- a/internal/stage_clone_repository.go +++ b/internal/stage_clone_repository.go @@ -78,6 +78,8 @@ func randomRepo() TestRepo { } func testCloneRepository(stageHarness *tester_utils.StageHarness) error { + initRandom() + logger := stageHarness.Logger executable := stageHarness.Executable diff --git a/internal/stage_create_blob.go b/internal/stage_create_blob.go index d725f58..55f8db0 100644 --- a/internal/stage_create_blob.go +++ b/internal/stage_create_blob.go @@ -12,6 +12,8 @@ import ( ) func testCreateBlob(stageHarness *tester_utils.StageHarness) error { + initRandom() + logger := stageHarness.Logger executable := stageHarness.Executable diff --git a/internal/stage_create_commit.go b/internal/stage_create_commit.go index 23736d4..637f1a7 100644 --- a/internal/stage_create_commit.go +++ b/internal/stage_create_commit.go @@ -15,6 +15,8 @@ import ( ) func testCreateCommit(stageHarness *tester_utils.StageHarness) error { + initRandom() + logger := stageHarness.Logger executable := stageHarness.Executable diff --git a/internal/stage_helpers.go b/internal/stage_helpers.go index a7d3bac..99f5bc5 100644 --- a/internal/stage_helpers.go +++ b/internal/stage_helpers.go @@ -2,6 +2,8 @@ package internal import ( "math/rand" + "os" + "strconv" "strings" "time" ) @@ -19,8 +21,18 @@ var randomWords = []string{ "vanilla", } -func init() { - rand.Seed(time.Now().UnixNano()) +func initRandom() { + if seed := os.Getenv("CODECRAFTERS_RANDOM_SEED"); seed != "" { + seedInt, err := strconv.Atoi(seed) + + if err != nil { + panic(err) + } + + rand.Seed(int64(seedInt)) + } else { + rand.Seed(time.Now().UnixNano()) + } } func randomWord() string { diff --git a/internal/stage_init.go b/internal/stage_init.go index f73cfa6..7c90b43 100644 --- a/internal/stage_init.go +++ b/internal/stage_init.go @@ -6,10 +6,13 @@ import ( "os" "path" + logger "github.com/codecrafters-io/tester-utils/logger" tester_utils "github.com/codecrafters-io/tester-utils" ) func testInit(stageHarness *tester_utils.StageHarness) error { + initRandom() + logger := stageHarness.Logger executable := stageHarness.Executable @@ -88,12 +91,12 @@ func assertFileExistsInDir(parent string, child string) error { return nil } -func logDebugTree(logger *tester_utils.Logger, dir string) { +func logDebugTree(logger *logger.Logger, dir string) { logger.Debugf("Files found in directory: ") doLogDebugTree(logger, dir, " ") } -func doLogDebugTree(logger *tester_utils.Logger, dir string, prefix string) { +func doLogDebugTree(logger *logger.Logger, dir string, prefix string) { entries, err := ioutil.ReadDir(dir) if err != nil { panic(err) diff --git a/internal/stage_read_blob.go b/internal/stage_read_blob.go index 68b89e1..1fcf432 100644 --- a/internal/stage_read_blob.go +++ b/internal/stage_read_blob.go @@ -16,6 +16,8 @@ import ( ) func testReadBlob(stageHarness *tester_utils.StageHarness) error { + initRandom() + logger := stageHarness.Logger executable := stageHarness.Executable diff --git a/internal/stage_read_tree.go b/internal/stage_read_tree.go index 414b86c..f04df3f 100644 --- a/internal/stage_read_tree.go +++ b/internal/stage_read_tree.go @@ -18,6 +18,8 @@ import ( ) func testReadTree(stageHarness *tester_utils.StageHarness) error { + initRandom() + logger := stageHarness.Logger executable := stageHarness.Executable diff --git a/internal/stage_write_tree.go b/internal/stage_write_tree.go index 3328822..83dd15c 100644 --- a/internal/stage_write_tree.go +++ b/internal/stage_write_tree.go @@ -8,6 +8,7 @@ import ( "math/rand" "os" "os/exec" + "strconv" "strings" "time" @@ -15,6 +16,8 @@ import ( ) func testWriteTree(stageHarness *tester_utils.StageHarness) error { + initRandom() + logger := stageHarness.Logger executable := stageHarness.Executable @@ -33,7 +36,20 @@ func testWriteTree(stageHarness *tester_utils.StageHarness) error { logger.Debugf("Creating some files & directories") - seed := time.Now().UnixNano() + var seed int64 + + if seedStr := os.Getenv("CODECRAFTERS_RANDOM_SEED"); seedStr != "" { + seedInt, err := strconv.Atoi(seedStr) + + if err != nil { + panic(err) + } + + seed = int64(seedInt) + } else { + seed = time.Now().UnixNano() + } + err = generateFiles(tempDir, seed) if err != nil { panic(err) diff --git a/internal/stages_test.go b/internal/stages_test.go index ff5a9ab..c894cf6 100644 --- a/internal/stages_test.go +++ b/internal/stages_test.go @@ -1,157 +1,56 @@ package internal import ( - "fmt" "os" - "path" "testing" - "github.com/stretchr/testify/assert" + tester_utils_testing "github.com/codecrafters-io/tester-utils/testing" ) -func TestInit(t *testing.T) { - m := NewStdIOMocker() - m.Start() - defer m.End() - - exitCode := runCLIStage("init", "test_helpers/stages/init_failure") - if !assert.Equal(t, 1, exitCode) { - failWithMockerOutput(t, m) - } - assert.Contains(t, m.ReadStdout(), "nothing") - assert.Contains(t, m.ReadStdout(), "Test failed") - - m.Reset() - - exitCode = runCLIStage("init", "test_helpers/stages/init") - if !assert.Equal(t, 0, exitCode) { - failWithMockerOutput(t, m) - } -} - -func TestReadBlob(t *testing.T) { - m := NewStdIOMocker() - m.Start() - defer m.End() - - exitCode := runCLIStage("read_blob", "test_helpers/stages/init") - if !assert.Equal(t, 1, exitCode) { - failWithMockerOutput(t, m) - } - if !assert.Contains(t, m.ReadStdout(), "Expected") { - failWithMockerOutput(t, m) - } - if !assert.Contains(t, m.ReadStdout(), "as stdout") { - failWithMockerOutput(t, m) - } - if !assert.Contains(t, m.ReadStdout(), "Test failed") { - failWithMockerOutput(t, m) - } - - m.Reset() - - exitCode = runCLIStage("read_blob", "test_helpers/stages/read_blob") - if !assert.Equal(t, 0, exitCode) { - failWithMockerOutput(t, m) - } -} - -func TestCreateBlob(t *testing.T) { - m := NewStdIOMocker() - m.Start() - defer m.End() - - exitCode := runCLIStage("create_blob", "test_helpers/stages/read_blob") - if !assert.Equal(t, 1, exitCode) { - failWithMockerOutput(t, m) - } - assert.Contains(t, m.ReadStdout(), "Expected") - assert.Contains(t, m.ReadStdout(), "Test failed") - - m.Reset() - - exitCode = runCLIStage("create_blob", "test_helpers/stages/create_blob") - if !assert.Equal(t, 0, exitCode) { - failWithMockerOutput(t, m) - } -} - -func TestReadTree(t *testing.T) { - m := NewStdIOMocker() - m.Start() - defer m.End() - - exitCode := runCLIStage("read_tree", "test_helpers/stages/read_blob") - if !assert.Equal(t, 1, exitCode) { - failWithMockerOutput(t, m) - } - assert.Contains(t, m.ReadStdout(), "Expected") - assert.Contains(t, m.ReadStdout(), "Test failed") - - m.Reset() - - exitCode = runCLIStage("read_tree", "test_helpers/stages/read_tree") - if !assert.Equal(t, 0, exitCode) { - failWithMockerOutput(t, m) - } -} - -func TestWriteTree(t *testing.T) { - m := NewStdIOMocker() - m.Start() - defer m.End() - - exitCode := runCLIStage("write_tree", "test_helpers/stages/read_tree") - if !assert.Equal(t, 1, exitCode) { - failWithMockerOutput(t, m) - } - assert.Contains(t, m.ReadStdout(), "Expected") - assert.Contains(t, m.ReadStdout(), "Test failed") - - m.Reset() - - exitCode = runCLIStage("write_tree", "test_helpers/stages/write_tree") - if !assert.Equal(t, 0, exitCode) { - failWithMockerOutput(t, m) - } -} - -func TestCreateCommit(t *testing.T) { - m := NewStdIOMocker() - m.Start() - defer m.End() - - exitCode := runCLIStage("create_commit", "test_helpers/stages/write_tree") - if !assert.Equal(t, 1, exitCode) { - failWithMockerOutput(t, m) - } - assert.Contains(t, m.ReadStdout(), "Expected") - assert.Contains(t, m.ReadStdout(), "Test failed") - - m.Reset() - - t.Skip("Not done yet") - - exitCode = runCLIStage("create_commit", "test_helpers/stages/create_commit") - if !assert.Equal(t, 0, exitCode) { - failWithMockerOutput(t, m) - } -} - -func runCLIStage(slug string, dir string) (exitCode int) { - cwd, err := os.Getwd() - if err != nil { - panic(err) - } - return RunCLI(map[string]string{ - "CODECRAFTERS_CURRENT_STAGE_SLUG": slug, - "CODECRAFTERS_SUBMISSION_DIR": path.Join(cwd, dir), - "CODECRAFTERS_COURSE_PAGE_URL": "test", - }) -} - -func failWithMockerOutput(t *testing.T, m *IOMocker) { - m.End() - t.Error(fmt.Sprintf("stdout: \n%s\n\nstderr: \n%s", m.ReadStdout(), m.ReadStderr())) - t.FailNow() +func TestStages(t *testing.T) { + os.Setenv("CODECRAFTERS_RANDOM_SEED", "1234567890") + + testCases := map[string]tester_utils_testing.TesterOutputTestCase{ + "init_success": { + UntilStageSlug: "init", + CodePath: "./test_helpers/stages/init", + ExpectedExitCode: 0, + StdoutFixturePath: "./test_helpers/fixtures/init", + NormalizeOutputFunc: normalizeTesterOutput, + }, + "read_blob_success": { + UntilStageSlug: "read_blob", + CodePath: "./test_helpers/stages/read_blob", + ExpectedExitCode: 0, + StdoutFixturePath: "./test_helpers/fixtures/read_blob", + NormalizeOutputFunc: normalizeTesterOutput, + }, + "create_blob_success": { + UntilStageSlug: "create_blob", + CodePath: "./test_helpers/stages/create_blob", + ExpectedExitCode: 0, + StdoutFixturePath: "./test_helpers/fixtures/create_blob", + NormalizeOutputFunc: normalizeTesterOutput, + }, + "read_tree_success": { + UntilStageSlug: "read_tree", + CodePath: "./test_helpers/stages/read_tree", + ExpectedExitCode: 0, + StdoutFixturePath: "./test_helpers/fixtures/read_tree", + NormalizeOutputFunc: normalizeTesterOutput, + }, + "write_tree_success": { + UntilStageSlug: "write_tree", + CodePath: "./test_helpers/stages/write_tree", + ExpectedExitCode: 0, + StdoutFixturePath: "./test_helpers/fixtures/write_tree", + NormalizeOutputFunc: normalizeTesterOutput, + }, + } + + tester_utils_testing.TestTesterOutput(t, testerDefinition, testCases) +} + +func normalizeTesterOutput(testerOutput []byte) []byte { + return testerOutput } diff --git a/internal/test_helpers/fixtures/create_blob b/internal/test_helpers/fixtures/create_blob new file mode 100644 index 0000000..46a01d5 --- /dev/null +++ b/internal/test_helpers/fixtures/create_blob @@ -0,0 +1,23 @@ +Debug = true + +[stage-1] Running tests for Stage #1: init +[stage-1] Running git init +[your_program] Initialized git directory +[stage-1] Test passed. + +[stage-2] Running tests for Stage #2: read_blob +[stage-2] Running ./your_git.sh init +[your_program] Initialized git directory +[stage-2] Writing sample file +[stage-2] Running ./your_git.sh cat-file -p 32eb24247f1cc0f2aa44da05b849392063a9b9e7 +[your_program] dumpty dooby doo donkey horsey vanilla +[stage-2] Test passed. + +[stage-3] Running tests for Stage #3: create_blob +[stage-3] Running ./your_git.sh init +[your_program] Initialized git directory +[stage-3] Writing sample file +[stage-3] Running ./your_git.sh hash-object -w donkey.txt +[your_program] 32eb24247f1cc0f2aa44da05b849392063a9b9e7 +[stage-3] Running git cat-file -p 32eb24247f1cc0f2aa44da05b849392063a9b9e7 +[stage-3] Test passed. diff --git a/internal/test_helpers/fixtures/init b/internal/test_helpers/fixtures/init new file mode 100644 index 0000000..6219ba7 --- /dev/null +++ b/internal/test_helpers/fixtures/init @@ -0,0 +1,6 @@ +Debug = true + +[stage-1] Running tests for Stage #1: init +[stage-1] Running git init +[your_program] Initialized git directory +[stage-1] Test passed. diff --git a/internal/test_helpers/fixtures/read_blob b/internal/test_helpers/fixtures/read_blob new file mode 100644 index 0000000..1c52978 --- /dev/null +++ b/internal/test_helpers/fixtures/read_blob @@ -0,0 +1,14 @@ +Debug = true + +[stage-1] Running tests for Stage #1: init +[stage-1] Running git init +[your_program] Initialized git directory +[stage-1] Test passed. + +[stage-2] Running tests for Stage #2: read_blob +[stage-2] Running ./your_git.sh init +[your_program] Initialized git directory +[stage-2] Writing sample file +[stage-2] Running ./your_git.sh cat-file -p 32eb24247f1cc0f2aa44da05b849392063a9b9e7 +[your_program] dumpty dooby doo donkey horsey vanilla +[stage-2] Test passed. diff --git a/internal/test_helpers/fixtures/read_tree b/internal/test_helpers/fixtures/read_tree new file mode 100644 index 0000000..559cbd5 --- /dev/null +++ b/internal/test_helpers/fixtures/read_tree @@ -0,0 +1,33 @@ +Debug = true + +[stage-1] Running tests for Stage #1: init +[stage-1] Running git init +[your_program] Initialized git directory +[stage-1] Test passed. + +[stage-2] Running tests for Stage #2: read_blob +[stage-2] Running ./your_git.sh init +[your_program] Initialized git directory +[stage-2] Writing sample file +[stage-2] Running ./your_git.sh cat-file -p 32eb24247f1cc0f2aa44da05b849392063a9b9e7 +[your_program] dumpty dooby doo donkey horsey vanilla +[stage-2] Test passed. + +[stage-3] Running tests for Stage #3: create_blob +[stage-3] Running ./your_git.sh init +[your_program] Initialized git directory +[stage-3] Writing sample file +[stage-3] Running ./your_git.sh hash-object -w donkey.txt +[your_program] 32eb24247f1cc0f2aa44da05b849392063a9b9e7 +[stage-3] Running git cat-file -p 32eb24247f1cc0f2aa44da05b849392063a9b9e7 +[stage-3] Test passed. + +[stage-4] Running tests for Stage #4: read_tree +[stage-4] Running ./your_git.sh init +[your_program] Initialized git directory +[stage-4] Writing a tree to git storage.. +[stage-4] Running ./your_git.sh ls-tree --name-only 978c60262a761655b8429dbeb12f7dce351c6cd6 +[your_program] humpty +[your_program] scooby +[your_program] vanilla +[stage-4] Test passed. diff --git a/internal/test_helpers/fixtures/write_tree b/internal/test_helpers/fixtures/write_tree new file mode 100644 index 0000000..9462792 --- /dev/null +++ b/internal/test_helpers/fixtures/write_tree @@ -0,0 +1,42 @@ +Debug = true + +[stage-1] Running tests for Stage #1: init +[stage-1] Running git init +[your_program] Initialized git directory +[stage-1] Test passed. + +[stage-2] Running tests for Stage #2: read_blob +[stage-2] Running ./your_git.sh init +[your_program] Initialized git directory +[stage-2] Writing sample file +[stage-2] Running ./your_git.sh cat-file -p 32eb24247f1cc0f2aa44da05b849392063a9b9e7 +[your_program] dumpty dooby doo donkey horsey vanilla +[stage-2] Test passed. + +[stage-3] Running tests for Stage #3: create_blob +[stage-3] Running ./your_git.sh init +[your_program] Initialized git directory +[stage-3] Writing sample file +[stage-3] Running ./your_git.sh hash-object -w donkey.txt +[your_program] 32eb24247f1cc0f2aa44da05b849392063a9b9e7 +[stage-3] Running git cat-file -p 32eb24247f1cc0f2aa44da05b849392063a9b9e7 +[stage-3] Test passed. + +[stage-4] Running tests for Stage #4: read_tree +[stage-4] Running ./your_git.sh init +[your_program] Initialized git directory +[stage-4] Writing a tree to git storage.. +[stage-4] Running ./your_git.sh ls-tree --name-only 978c60262a761655b8429dbeb12f7dce351c6cd6 +[your_program] humpty +[your_program] scooby +[your_program] vanilla +[stage-4] Test passed. + +[stage-5] Running tests for Stage #5: write_tree +[stage-5] Running ./your_git.sh init +[your_program] Initialized git directory +[stage-5] Creating some files & directories +[stage-5] Running ./your_git.sh write-tree +[your_program] 7bba7df6498a0391b69e6343b517c3360b658293 +[stage-5] Running git ls-tree --name-only  +[stage-5] Test passed. diff --git a/internal/tester_definition.go b/internal/tester_definition.go index 81b28ac..20f5051 100644 --- a/internal/tester_definition.go +++ b/internal/tester_definition.go @@ -5,57 +5,36 @@ import ( ) var testerDefinition = testerutils.TesterDefinition{ - AntiCheatStages: []testerutils.Stage{}, + AntiCheatTestCases: []testerutils.TestCase{}, ExecutableFileName: "your_git.sh", - Stages: []testerutils.Stage{ + TestCases: []testerutils.TestCase{ { - Number: 1, Slug: "init", - Title: "Initialize the .git directory", TestFunc: testInit, - ShouldRunPreviousStages: true, }, { - Number: 2, Slug: "read_blob", - Title: "Read a blob object", TestFunc: testReadBlob, - ShouldRunPreviousStages: true, }, { - Number: 3, Slug: "create_blob", - Title: "Create a blob object", TestFunc: testCreateBlob, - ShouldRunPreviousStages: true, }, { - Number: 4, Slug: "read_tree", - Title: "Read a tree object", TestFunc: testReadTree, - ShouldRunPreviousStages: true, }, { - Number: 5, Slug: "write_tree", - Title: "Write a tree object", TestFunc: testWriteTree, - ShouldRunPreviousStages: true, }, { - Number: 6, Slug: "create_commit", - Title: "Create a commit", TestFunc: testCreateCommit, - ShouldRunPreviousStages: true, }, { - Number: 7, Slug: "clone_repository", - Title: "Clone a repository", TestFunc: testCloneRepository, - ShouldRunPreviousStages: true, }, }, } diff --git a/internal/tester_definition_test.go b/internal/tester_definition_test.go index c017ceb..ce3b55f 100644 --- a/internal/tester_definition_test.go +++ b/internal/tester_definition_test.go @@ -2,8 +2,10 @@ package internal import ( "testing" + + tester_utils_testing "github.com/codecrafters-io/tester-utils/testing" ) func TestStagesMatchYAML(t *testing.T) { - testerDefinition.TestAgainstYAML(t, "test_helpers/course_definition.yml") + tester_utils_testing.ValidateTesterDefinitionAgainstYAML(t, testerDefinition, "test_helpers/course_definition.yml") }