-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Begin link * Make normalise public * Improve link tests * Remove outdated delete.me * Update README * Update error messages and tests based on CI results
- Loading branch information
Showing
6 changed files
with
113 additions
and
4 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
Empty file.
Empty file.
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,42 @@ | ||
module main | ||
|
||
import common | ||
import os | ||
|
||
const app = common.CoreutilInfo{ | ||
name: 'link' | ||
description: 'Call the link function to create a link named FILE2 to an existing FILE1.' | ||
} | ||
|
||
struct Settings { | ||
mut: | ||
files []string | ||
} | ||
|
||
fn args() Settings { | ||
mut fp := app.make_flag_parser(os.args) | ||
mut st := Settings{} | ||
st.files = fp.remaining_parameters() | ||
match st.files.len { | ||
0 { app.quit(message: 'missing operand') } | ||
1 { app.quit(message: "missing operand after '${st.files[0]}'", show_help_advice: true) } | ||
2 {} | ||
else { app.quit(message: "extra operand '${st.files[2]}'", show_help_advice: true) } | ||
} | ||
|
||
return st | ||
} | ||
|
||
fn link(settings Settings) !int { | ||
mut exit_code := 0 | ||
os.link(settings.files[0], settings.files[1]) or { | ||
app.quit( | ||
message: "cannot create link '${settings.files[1]}' to '${settings.files[0]}': ${err.msg()}" | ||
) | ||
} | ||
return exit_code | ||
} | ||
|
||
fn main() { | ||
exit(link(args()) or { common.err_programming_error }) | ||
} |
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,67 @@ | ||
module main | ||
|
||
import common.testing | ||
import os | ||
|
||
const rig = testing.prepare_rig(util: 'link') | ||
|
||
fn testsuite_begin() { | ||
} | ||
|
||
fn testsuite_end() { | ||
} | ||
|
||
fn test_help_and_version() { | ||
rig.assert_help_and_version_options_work() | ||
} | ||
|
||
fn test_compare() { | ||
$if windows { | ||
// The coreutils used on Windows does not produce the exact | ||
// same error messages | ||
rig.assert_same_exit_code('a') | ||
rig.assert_same_exit_code('a b') | ||
rig.assert_same_exit_code('a b c') | ||
rig.assert_same_exit_code('a b c d e f') | ||
os.mkdir('d')! | ||
rig.assert_same_exit_code('d e') | ||
os.rmdir('d')! | ||
} $else { | ||
rig.assert_same_results('a') | ||
rig.assert_same_results('a b') | ||
rig.assert_same_results('a b c') | ||
rig.assert_same_results('a b c d e f') | ||
os.mkdir('d')! | ||
rig.assert_same_results('d e') | ||
os.rmdir('d')! | ||
} | ||
|
||
// We can't use assert_same_results here because the hard link will already | ||
// exist when the second util is called | ||
os.write_file('a', '12345')! | ||
cmd1_res := rig.call_orig('a b') | ||
assert os.is_file('a') | ||
assert os.is_file('b') | ||
assert os.read_file('b') or { '' } == '12345' | ||
mut f := os.open_append('b')! | ||
f.write_string('67890')! | ||
f.close() | ||
assert os.read_file('a') or { '' } == '1234567890' | ||
os.rm('b')! | ||
os.rm('a')! | ||
|
||
os.write_file('a', '12345')! | ||
cmd2_res := rig.call_new('a b') | ||
assert os.is_file('a') | ||
assert os.is_file('b') | ||
assert os.read_file('b') or { '' } == '12345' | ||
f = os.open_append('b')! | ||
f.write_string('67890')! | ||
f.close() | ||
assert os.read_file('a') or { '' } == '1234567890' | ||
os.rm('b')! | ||
os.rm('a')! | ||
|
||
assert cmd1_res.exit_code == cmd2_res.exit_code | ||
assert testing.normalise(cmd1_res.output) == testing.normalise(cmd2_res.output) | ||
} |