Skip to content

Commit ffe0f04

Browse files
committed
add prolog config
1 parent dcdf168 commit ffe0f04

4 files changed

Lines changed: 73 additions & 8 deletions

File tree

workspace/exercise_config.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ var configFilepath = filepath.Join(ignoreSubdir, configFilename)
1515
// Note: we only use a subset of its fields
1616
type ExerciseConfig struct {
1717
Files struct {
18-
Test []string `json:"test"`
18+
Solution []string `json:"solution"`
19+
Test []string `json:"test"`
1920
} `json:"files"`
2021
}
2122

@@ -33,12 +34,24 @@ func NewExerciseConfig(dir string) (*ExerciseConfig, error) {
3334
return &config, nil
3435
}
3536

36-
// GetTestFiles finds returns the names of the files that hold unit tests for this exercise, if any
37+
// GetTestFiles finds returns the names of the file(s) that hold unit tests for this exercise, if any
38+
func (c *ExerciseConfig) GetSolutionFiles() ([]string, error) {
39+
result := c.Files.Solution
40+
if result == nil {
41+
// solution file(s) key was missing in config json, which is an error when calling this fuction
42+
return []string{}, errors.New("no `files.solution` key in your `config.json`. Was it removed by mistake?")
43+
}
44+
45+
return result, nil
46+
}
47+
48+
// GetTestFiles finds returns the names of the file(s) that hold unit tests for this exercise, if any
3749
func (c *ExerciseConfig) GetTestFiles() ([]string, error) {
38-
if c.Files.Test == nil {
39-
// test files key was missing in config json, which is an error when calling this fuction
50+
result := c.Files.Test
51+
if result == nil {
52+
// test file(s) key was missing in config json, which is an error when calling this fuction
4053
return []string{}, errors.New("no `files.test` key in your `config.json`. Was it removed by mistake?")
4154
}
4255

43-
return c.Files.Test, nil
56+
return result, nil
4457
}

workspace/exercise_config_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ func TestExerciseConfig(t *testing.T) {
2828
ec, err := NewExerciseConfig(dir)
2929
assert.NoError(t, err)
3030

31+
assert.Equal(t, ec.Files.Solution, []string{"lasagna.rb"})
32+
solutionFiles, err := ec.GetSolutionFiles()
33+
assert.NoError(t, err)
34+
assert.Equal(t, solutionFiles, []string{"lasagna.rb"})
35+
3136
assert.Equal(t, ec.Files.Test, []string{"lasagna_test.rb"})
3237
testFiles, err := ec.GetTestFiles()
3338
assert.NoError(t, err)
@@ -45,12 +50,14 @@ func TestExerciseConfigNoTestKey(t *testing.T) {
4550
f, err := os.Create(filepath.Join(dir, ".exercism", "config.json"))
4651
assert.NoError(t, err)
4752

48-
_, err = f.WriteString(`{ "blurb": "Learn about the basics of Ruby by following a lasagna recipe.", "authors": ["iHiD", "pvcarrera"], "files": { "solution": ["lasagna.rb"], "exemplar": [".meta/exemplar.rb"] } } `)
53+
_, err = f.WriteString(`{ "blurb": "Learn about the basics of Ruby by following a lasagna recipe.", "authors": ["iHiD", "pvcarrera"], "files": { "exemplar": [".meta/exemplar.rb"] } } `)
4954
assert.NoError(t, err)
5055

5156
ec, err := NewExerciseConfig(dir)
5257
assert.NoError(t, err)
5358

59+
_, err = ec.GetSolutionFiles()
60+
assert.Error(t, err, "no `files.solution` key in your `config.json`")
5461
_, err = ec.GetTestFiles()
5562
assert.Error(t, err, "no `files.test` key in your `config.json`")
5663
}

workspace/test_configurations.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package workspace
22

33
import (
4+
"fmt"
45
"runtime"
56
"strings"
67
)
@@ -22,12 +23,32 @@ func (c *TestConfiguration) GetTestCommand() (string, error) {
2223
cmd = c.Command
2324
}
2425

25-
if strings.Contains(cmd, "{{test_files}}") {
26-
exerciseConfig, err := NewExerciseConfig(".")
26+
// pre-declare these so we can conditionally initialize them
27+
var exerciseConfig *ExerciseConfig
28+
var err error
29+
30+
if strings.Contains(cmd, "{{") {
31+
// only read exercise's config.json if we need it
32+
exerciseConfig, err = NewExerciseConfig(".")
2733
if err != nil {
2834
return "", err
2935
}
36+
}
3037

38+
if strings.Contains(cmd, "{{solution_files}}") {
39+
if exerciseConfig == nil {
40+
return "", fmt.Errorf("exerciseConfig not initialize before use")
41+
}
42+
solutionFiles, err := exerciseConfig.GetSolutionFiles()
43+
if err != nil {
44+
return "", err
45+
}
46+
cmd = strings.ReplaceAll(cmd, "{{solution_files}}", strings.Join(solutionFiles, " "))
47+
}
48+
if strings.Contains(cmd, "{{test_files}}") {
49+
if exerciseConfig == nil {
50+
return "", fmt.Errorf("exerciseConfig not initialize before use")
51+
}
3152
testFiles, err := exerciseConfig.GetTestFiles()
3253
if err != nil {
3354
return "", err
@@ -146,6 +167,9 @@ var TestConfigurations = map[string]TestConfiguration{
146167
"php": {
147168
Command: "phpunit {{test_files}}",
148169
},
170+
"prolog": {
171+
Command: "swipl -f {{solution_files}} -s {{test_files}} -g run_tests,halt -t 'halt(1)'",
172+
},
149173
"purescript": {
150174
Command: "spago test",
151175
},

workspace/test_configurations_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ func TestGetCommandMissingConfig(t *testing.T) {
4949
assert.Contains(t, err.Error(), path.Join(".exercism", "config.json:"))
5050
}
5151

52+
func TestIncludesSolutionAndTestFilesInCommand(t *testing.T) {
53+
testConfig, ok := TestConfigurations["prolog"]
54+
assert.True(t, ok, "unexpectedly unable to find prolog test config")
55+
56+
// this creates a config file in the test directory and removes it
57+
dir := filepath.Join(".", ".exercism")
58+
err := os.Mkdir(dir, os.ModePerm)
59+
assert.NoError(t, err)
60+
defer os.RemoveAll(dir)
61+
62+
f, err := os.Create(filepath.Join(dir, "config.json"))
63+
assert.NoError(t, err)
64+
65+
_, err = f.WriteString(`{ "blurb": "Learn about the basics of Prolog by following a lasagna recipe.", "authors": ["iHiD", "pvcarrera"], "files": { "solution": ["lasagna.pl"], "test": ["lasagna_tests.plt"] } } `)
66+
assert.NoError(t, err)
67+
68+
cmd, err := testConfig.GetTestCommand()
69+
assert.NoError(t, err)
70+
assert.Equal(t, cmd, "swipl -f lasagna.pl -s lasagna_tests.plt -g run_tests,halt -t 'halt(1)'")
71+
}
72+
5273
func TestIncludesTestFilesInCommand(t *testing.T) {
5374
testConfig, ok := TestConfigurations["ruby"]
5475
assert.True(t, ok, "unexpectedly unable to find ruby test config")

0 commit comments

Comments
 (0)