Skip to content

Commit

Permalink
Merge pull request #536 from briandowns/add_more_functions_to_system
Browse files Browse the repository at this point in the history
Add mkdirTemp to System module
  • Loading branch information
Jason2605 committed Oct 6, 2022
2 parents 380070d + baf5175 commit 1908cdd
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/docs/standard-lib/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,19 @@ Note: This is not available on Windows systems.

```cs
System.uname();
```

### System.mkdirTemp(string: directory_template -> optional)

Makes a temporary directory. If an empty string is given, the temporary directory's name will be a random string created in the current working directory. If a string is passed in, the temporary directory will be created with that name in the current working directory.

The directory template passed in **must** end with "XXXXXX".

Returns a Result type and on success will unwrap to a the created directory name.

Note: This is not available on Windows systems.

```cs
System.mkdirTemp().unwrap(); // "VOO16s"
System.mkdirTemp("test_XXXXXX").unwrap(); // "test_0bL2qS"
```
8 changes: 8 additions & 0 deletions examples/mkdirTemp.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Env;
import System;

const tmpDir = Env.get("TMPDIR") + "XXXXXX";

var res = System.mkdirTemp(tmpDir);

print(res);
40 changes: 40 additions & 0 deletions src/optionals/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,45 @@ static Value unameNative(DictuVM *vm, int argCount, Value *args) {

return OBJ_VAL(unameDict);
}

static Value mkdirTempNative(DictuVM *vm, int argCount, Value *args) {
if (argCount > 1) {
runtimeError(vm, "mkdirTemp() takes 0 or 1 argument(s) (%d given)", argCount);
return EMPTY_VAL;
}

char *template = "XXXXXX";

if (argCount == 1) {
if (!IS_STRING(args[0])) {
runtimeError(vm, "mkdirTemp() first argument must be a string");
return EMPTY_VAL;
}

template = AS_CSTRING(args[0]);
}

char *tmpl = {0};
int size;

if (template[0] != '\0') {
size = strlen(template) + 1;
tmpl = ALLOCATE(vm, char, size);
strcpy(tmpl, template);
} else {
size = 7;
tmpl = ALLOCATE(vm, char, size);
strcpy(tmpl, "XXXXXX");
}

char *tmpDir = mkdtemp(tmpl);
if (!tmpDir) {
FREE_ARRAY(vm, char, tmpl, size);
ERROR_RESULT;
}

return newResultSuccess(vm, OBJ_VAL(takeString(vm, tmpDir, size - 1)));
}
#endif

static Value rmdirNative(DictuVM *vm, int argCount, Value *args) {
Expand Down Expand Up @@ -477,6 +516,7 @@ Value createSystemModule(DictuVM *vm) {
defineNative(vm, &module->values, "getpid", getpidNative);
defineNative(vm, &module->values, "chown", chownNative);
defineNative(vm, &module->values, "uname", unameNative);
defineNative(vm, &module->values, "mkdirTemp", mkdirTempNative);
#endif
defineNative(vm, &module->values, "rmdir", rmdirNative);
defineNative(vm, &module->values, "mkdir", mkdirNative);
Expand Down
1 change: 1 addition & 0 deletions tests/system/import.du
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ import "constants.du";
import "chmod.du";
import "chown.du";
import "uname.du";
import "mkdirTemp.du";
40 changes: 40 additions & 0 deletions tests/system/mkdirTemp.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* mkdirTemp.du
*
* Testing the System.mkdirTemp function
*
* mkdirTemp makes a temporary directory.
*/
from UnitTest import UnitTest;

import Path;
import System;

class TestSystemMkdirTest < UnitTest {
testSystemMkdir(tempDirName) {
if (System.platform != "windows") {
if (Path.exists(tempDirName)) {
this.assertSuccess(System.rmdir(tempDirName));
}

System.mkdirTemp(tempDirName).match(
def (result) => {
this.assertEquals(result[0:-6], tempDirName[0:-6]);
this.assertSuccess(System.rmdir(result));
},
def (error) => {
this.assertEquals(error, "Invalid argument");
}
);
}
}

testSystemMkdirProvider() {
return [
'test_mkdir_temp',
'test_mkdir_tempXXXXXX'
];
}
}

TestSystemMkdirTest().run();

0 comments on commit 1908cdd

Please sign in to comment.