From ecf0f0bb5d91c2b1e202c616cdc7d17024c89c2a Mon Sep 17 00:00:00 2001 From: Aleksander Einarsen <aleksace@stud.ntnu.no> Date: Wed, 9 Oct 2024 22:55:14 +0200 Subject: [PATCH] Make Command now gets the runtime OS inside the function, instead of it being passed as an argument --- modules/compiler_V2/data/OS.go | 2 +- .../compiler_V2/go-compiler2/go_compiler.go | 15 ++++--------- .../go-compiler2/go_compiler_test.go | 3 +-- .../rust-compiler2/rust_compiler.go | 21 +++++++------------ .../rust-compiler2/rust_compiler_test.go | 3 +-- modules/compiler_V2/utils/make_command.go | 5 ++++- 6 files changed, 18 insertions(+), 31 deletions(-) diff --git a/modules/compiler_V2/data/OS.go b/modules/compiler_V2/data/OS.go index b1f2338..ee712af 100644 --- a/modules/compiler_V2/data/OS.go +++ b/modules/compiler_V2/data/OS.go @@ -1,6 +1,6 @@ package data -type OS string +type OS = string const ( Windows OS = "windows" diff --git a/modules/compiler_V2/go-compiler2/go_compiler.go b/modules/compiler_V2/go-compiler2/go_compiler.go index ead1ae1..2e70452 100644 --- a/modules/compiler_V2/go-compiler2/go_compiler.go +++ b/modules/compiler_V2/go-compiler2/go_compiler.go @@ -2,23 +2,16 @@ package go_compiler2 import ( "compiler_V2/consts" - "compiler_V2/data" "compiler_V2/utils" "os" ) const fileName = "main.go" -type GoCompiler struct { - OS data.OS - Language data.Language -} +type GoCompiler struct{} -func NewGoCompiler(OS data.OS) *GoCompiler { - return &GoCompiler{ - OS: OS, - Language: data.Go, - } +func NewGoCompiler() *GoCompiler { + return &GoCompiler{} } 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 //cmdSlice := strings.Fields(cmdString) - cmd := utils.MakeCommand(gb.OS, cmdString) + cmd := utils.MakeCommand(cmdString) cmd.Dir = consts.TempOutputDir return cmd.CombinedOutput() } diff --git a/modules/compiler_V2/go-compiler2/go_compiler_test.go b/modules/compiler_V2/go-compiler2/go_compiler_test.go index ab38ecc..16b9dc5 100644 --- a/modules/compiler_V2/go-compiler2/go_compiler_test.go +++ b/modules/compiler_V2/go-compiler2/go_compiler_test.go @@ -1,7 +1,6 @@ package go_compiler2 import ( - "compiler_V2/data" "os" "testing" ) @@ -35,7 +34,7 @@ func TestCompileStringToGo(t *testing.T) { // Read the code from the file code, err := os.ReadFile(test.filename) - output, err := NewGoCompiler(data.Windows).CheckCompileErrors(code) + output, err := NewGoCompiler().CheckCompileErrors(code) if err != nil && test.shouldCompile { t.Errorf("Expected the code to compile, but got an output: %v \n error: %v", string(output), err) diff --git a/modules/compiler_V2/rust-compiler2/rust_compiler.go b/modules/compiler_V2/rust-compiler2/rust_compiler.go index 52f18b4..0f92af6 100644 --- a/modules/compiler_V2/rust-compiler2/rust_compiler.go +++ b/modules/compiler_V2/rust-compiler2/rust_compiler.go @@ -2,7 +2,6 @@ package rust_compiler2 import ( "compiler_V2/consts" - "compiler_V2/data" "compiler_V2/utils" "os" "strings" @@ -10,16 +9,10 @@ import ( const fileName = "main.rs" -type RustCompiler struct { - OS data.OS - Language data.Language -} +type RustCompiler struct{} -func NewRustCompiler(OS data.OS) *RustCompiler { - return &RustCompiler{ - OS: OS, - Language: data.Rust, - } +func NewRustCompiler() *RustCompiler { + return &RustCompiler{} } func (gb *RustCompiler) CheckCompileErrors(srcCode []byte, dependencies ...string) ([]byte, error) { @@ -28,7 +21,7 @@ func (gb *RustCompiler) CheckCompileErrors(srcCode []byte, dependencies ...strin defer utils.RemoveTempFolders(consts.TempOutputDir) // Init cargo - if err := initCargo(gb.OS); err != nil { + if err := initCargo(); err != nil { return nil, err } @@ -47,14 +40,14 @@ func (gb *RustCompiler) CheckCompileErrors(srcCode []byte, dependencies ...strin cmdString += " cargo check" //cmdSlice := strings.Fields(cmdString) - cmd := utils.MakeCommand(gb.OS, cmdString) + cmd := utils.MakeCommand(cmdString) cmd.Dir = consts.TempOutputDir return cmd.CombinedOutput() } -func initCargo(OS data.OS) error { +func initCargo() error { // Init cargo - cmd := utils.MakeCommand(OS, "cargo init --bin") + cmd := utils.MakeCommand("cargo init --bin") cmd.Dir = consts.TempOutputDir return cmd.Run() } diff --git a/modules/compiler_V2/rust-compiler2/rust_compiler_test.go b/modules/compiler_V2/rust-compiler2/rust_compiler_test.go index 87533db..6fc5cda 100644 --- a/modules/compiler_V2/rust-compiler2/rust_compiler_test.go +++ b/modules/compiler_V2/rust-compiler2/rust_compiler_test.go @@ -1,7 +1,6 @@ package rust_compiler2 import ( - "compiler_V2/data" "os" "testing" ) @@ -35,7 +34,7 @@ func TestCompileStringToRust(t *testing.T) { // Read the code from the file 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 { t.Errorf("Expected the code to compile, but got an output: %v \n error: %v", string(output), err) diff --git a/modules/compiler_V2/utils/make_command.go b/modules/compiler_V2/utils/make_command.go index cffa76c..83f0ded 100644 --- a/modules/compiler_V2/utils/make_command.go +++ b/modules/compiler_V2/utils/make_command.go @@ -3,9 +3,12 @@ package utils import ( "compiler_V2/data" "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 { case data.Windows: return exec.Command("cmd", "/c", cmd) -- GitLab