Skip to content
Snippets Groups Projects
Commit ecf0f0bb authored by Aleksander Einarsen's avatar Aleksander Einarsen
Browse files

Make Command now gets the runtime OS inside the function, instead of it being passed as an argument

parent 58eef238
No related branches found
No related tags found
No related merge requests found
package data package data
type OS string type OS = string
const ( const (
Windows OS = "windows" Windows OS = "windows"
......
...@@ -2,23 +2,16 @@ package go_compiler2 ...@@ -2,23 +2,16 @@ package go_compiler2
import ( import (
"compiler_V2/consts" "compiler_V2/consts"
"compiler_V2/data"
"compiler_V2/utils" "compiler_V2/utils"
"os" "os"
) )
const fileName = "main.go" const fileName = "main.go"
type GoCompiler struct { type GoCompiler struct{}
OS data.OS
Language data.Language
}
func NewGoCompiler(OS data.OS) *GoCompiler { func NewGoCompiler() *GoCompiler {
return &GoCompiler{ return &GoCompiler{}
OS: OS,
Language: data.Go,
}
} }
func (gb *GoCompiler) CheckCompileErrors(srcCode []byte) ([]byte, error) { func (gb *GoCompiler) CheckCompileErrors(srcCode []byte) ([]byte, error) {
...@@ -38,7 +31,7 @@ func (gb *GoCompiler) CheckCompileErrors(srcCode []byte) ([]byte, error) { ...@@ -38,7 +31,7 @@ func (gb *GoCompiler) CheckCompileErrors(srcCode []byte) ([]byte, error) {
cmdString += " && go build -o main " + fileName cmdString += " && go build -o main " + fileName
//cmdSlice := strings.Fields(cmdString) //cmdSlice := strings.Fields(cmdString)
cmd := utils.MakeCommand(gb.OS, cmdString) cmd := utils.MakeCommand(cmdString)
cmd.Dir = consts.TempOutputDir cmd.Dir = consts.TempOutputDir
return cmd.CombinedOutput() return cmd.CombinedOutput()
} }
package go_compiler2 package go_compiler2
import ( import (
"compiler_V2/data"
"os" "os"
"testing" "testing"
) )
...@@ -35,7 +34,7 @@ func TestCompileStringToGo(t *testing.T) { ...@@ -35,7 +34,7 @@ func TestCompileStringToGo(t *testing.T) {
// Read the code from the file // Read the code from the file
code, err := os.ReadFile(test.filename) code, err := os.ReadFile(test.filename)
output, err := NewGoCompiler(data.Windows).CheckCompileErrors(code) output, err := NewGoCompiler().CheckCompileErrors(code)
if err != nil && test.shouldCompile { if err != nil && test.shouldCompile {
t.Errorf("Expected the code to compile, but got an output: %v \n error: %v", string(output), err) t.Errorf("Expected the code to compile, but got an output: %v \n error: %v", string(output), err)
......
...@@ -2,7 +2,6 @@ package rust_compiler2 ...@@ -2,7 +2,6 @@ package rust_compiler2
import ( import (
"compiler_V2/consts" "compiler_V2/consts"
"compiler_V2/data"
"compiler_V2/utils" "compiler_V2/utils"
"os" "os"
"strings" "strings"
...@@ -10,16 +9,10 @@ import ( ...@@ -10,16 +9,10 @@ import (
const fileName = "main.rs" const fileName = "main.rs"
type RustCompiler struct { type RustCompiler struct{}
OS data.OS
Language data.Language
}
func NewRustCompiler(OS data.OS) *RustCompiler { func NewRustCompiler() *RustCompiler {
return &RustCompiler{ return &RustCompiler{}
OS: OS,
Language: data.Rust,
}
} }
func (gb *RustCompiler) CheckCompileErrors(srcCode []byte, dependencies ...string) ([]byte, error) { func (gb *RustCompiler) CheckCompileErrors(srcCode []byte, dependencies ...string) ([]byte, error) {
...@@ -28,7 +21,7 @@ func (gb *RustCompiler) CheckCompileErrors(srcCode []byte, dependencies ...strin ...@@ -28,7 +21,7 @@ func (gb *RustCompiler) CheckCompileErrors(srcCode []byte, dependencies ...strin
defer utils.RemoveTempFolders(consts.TempOutputDir) defer utils.RemoveTempFolders(consts.TempOutputDir)
// Init cargo // Init cargo
if err := initCargo(gb.OS); err != nil { if err := initCargo(); err != nil {
return nil, err return nil, err
} }
...@@ -47,14 +40,14 @@ func (gb *RustCompiler) CheckCompileErrors(srcCode []byte, dependencies ...strin ...@@ -47,14 +40,14 @@ func (gb *RustCompiler) CheckCompileErrors(srcCode []byte, dependencies ...strin
cmdString += " cargo check" cmdString += " cargo check"
//cmdSlice := strings.Fields(cmdString) //cmdSlice := strings.Fields(cmdString)
cmd := utils.MakeCommand(gb.OS, cmdString) cmd := utils.MakeCommand(cmdString)
cmd.Dir = consts.TempOutputDir cmd.Dir = consts.TempOutputDir
return cmd.CombinedOutput() return cmd.CombinedOutput()
} }
func initCargo(OS data.OS) error { func initCargo() error {
// Init cargo // Init cargo
cmd := utils.MakeCommand(OS, "cargo init --bin") cmd := utils.MakeCommand("cargo init --bin")
cmd.Dir = consts.TempOutputDir cmd.Dir = consts.TempOutputDir
return cmd.Run() return cmd.Run()
} }
package rust_compiler2 package rust_compiler2
import ( import (
"compiler_V2/data"
"os" "os"
"testing" "testing"
) )
...@@ -35,7 +34,7 @@ func TestCompileStringToRust(t *testing.T) { ...@@ -35,7 +34,7 @@ func TestCompileStringToRust(t *testing.T) {
// Read the code from the file // Read the code from the file
code, err := os.ReadFile(test.filename) code, err := os.ReadFile(test.filename)
output, err := NewRustCompiler(data.Windows).CheckCompileErrors(code, test.dependencies...) output, err := NewRustCompiler().CheckCompileErrors(code, test.dependencies...)
if err != nil && test.shouldCompile { if err != nil && test.shouldCompile {
t.Errorf("Expected the code to compile, but got an output: %v \n error: %v", string(output), err) t.Errorf("Expected the code to compile, but got an output: %v \n error: %v", string(output), err)
......
...@@ -3,9 +3,12 @@ package utils ...@@ -3,9 +3,12 @@ package utils
import ( import (
"compiler_V2/data" "compiler_V2/data"
"os/exec" "os/exec"
"runtime"
) )
func MakeCommand(OS data.OS, cmd string) *exec.Cmd { // MakeCommand creates a command based on the runtime OS
func MakeCommand(cmd string) *exec.Cmd {
OS := runtime.GOOS
switch OS { switch OS {
case data.Windows: case data.Windows:
return exec.Command("cmd", "/c", cmd) return exec.Command("cmd", "/c", cmd)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment