-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from 0x20F/testable-refactor
Testable refactor
- Loading branch information
Showing
29 changed files
with
1,619 additions
and
279 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
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,88 @@ | ||
package carbon | ||
|
||
import ( | ||
"co2/types" | ||
"testing" | ||
) | ||
|
||
// Note that this is indented with spaces. | ||
// Tabs will break yaml so be careful. | ||
var customDocument = ` | ||
test: | ||
image: golang | ||
depends_on: | ||
- test-db | ||
--- | ||
test-db: | ||
image: lmao | ||
ports: | ||
- "8080:80" | ||
` | ||
|
||
func TestMovingDataBetweenDefinitions(t *testing.T) { | ||
// Create a new service definition | ||
def := types.ServiceDefinition{ | ||
"service": { | ||
"unique-but-useless": "value", | ||
}, | ||
} | ||
|
||
// Create a new carbon config | ||
config := types.CarbonConfig{ | ||
"service": {}, | ||
} | ||
|
||
// Move the data from the service definition into the carbon config | ||
k, v := move(def, config, "filename") | ||
|
||
// Assert that the key is the same as the service name | ||
if k != "service" { | ||
t.Errorf("Expected key to be 'service', got '%s'", k) | ||
} | ||
|
||
// Make sure the path got set to filename | ||
if v.Path != "filename" { | ||
t.Errorf("Expected path to be 'filename', got '%s'", v.Path) | ||
} | ||
|
||
// Make sure the name got set to service | ||
if v.Name != "service" { | ||
t.Errorf("Expected name to be 'service', got '%s'", v.Name) | ||
} | ||
|
||
// Make sure the unique-but-useless is present in the full contents | ||
if v.FullContents["unique-but-useless"] != "value" { | ||
t.Errorf("Expected 'unique-but-useless' to be 'value', got '%s'", v.FullContents["unique-but-useless"]) | ||
} | ||
} | ||
|
||
func TestYamlParsingOfMultipleDocuments(t *testing.T) { | ||
// Parse the yaml into a carbon config | ||
config := documents([]byte(customDocument), "filename") | ||
|
||
// Make sure the config has the correct number of services | ||
if len(config) != 2 { | ||
t.Errorf("Expected 2 services, got %d", len(config)) | ||
} | ||
|
||
// Make sure the path is filename for each of the services | ||
if config["test"].Path != "filename" { | ||
t.Errorf("Expected path to be 'filename', got '%s'", config["test"].Path) | ||
} | ||
|
||
if config["test-db"].Path != "filename" { | ||
t.Errorf("Expected path to be 'filename', got '%s'", config["test-db"].Path) | ||
} | ||
|
||
// Make sure the config has the correct number of fields | ||
if len(config["test"].FullContents) != 2 { | ||
t.Errorf("Expected 1 field, got %d", len(config["test"].FullContents)) | ||
} | ||
|
||
// Make sure the config has the correct number of fields | ||
if len(config["test-db"].FullContents) != 2 { | ||
t.Errorf("Expected 2 fields, got %d", len(config["test-db"].FullContents)) | ||
} | ||
} |
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,51 @@ | ||
package cmd | ||
|
||
import ( | ||
"co2/carbon" | ||
"co2/database" | ||
"co2/types" | ||
) | ||
|
||
var fs FsWrapper = &impl{} | ||
|
||
type FsWrapper interface { | ||
Services() types.CarbonConfig | ||
} | ||
|
||
type impl struct{} | ||
|
||
// Looks through all the registered stores and returns all | ||
// the carbon services that are defined within those stores. | ||
// | ||
// This will never to too deep into the stores when looking | ||
// for services since we want it to be fast. Usually a depth of 2 | ||
// is enough. | ||
// | ||
// Each of the returned configurations will have the store | ||
// they belong to injected as well so they can retrieve | ||
// the required data if ever needed. | ||
func (i *impl) Services() types.CarbonConfig { | ||
stores := database.Stores() | ||
configs := types.CarbonConfig{} | ||
|
||
for _, store := range stores { | ||
files := carbon.Configurations(store.Path, 2) | ||
|
||
for k, v := range files { | ||
v.Store = &store | ||
configs[k] = v | ||
} | ||
} | ||
|
||
return configs | ||
} | ||
|
||
// Replaces the default Fs instance with a custom | ||
// implementation. | ||
// | ||
// Note that this exists for the sole purpose of unit testing. | ||
// It makes it easy to replace how we access the carbon methods | ||
// during tests. | ||
func WrapFs(custom FsWrapper) { | ||
fs = custom | ||
} |
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
Oops, something went wrong.