diff options
author | George Roman <george.roman.99@gmail.com> | 2018-06-13 20:09:07 +0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-06-14 14:14:44 -0300 |
commit | c01c8a21a735c3a74270635e5bd843bac71bc624 (patch) | |
tree | 64218be890422d910632e0260bf4c48e02991b3e | |
parent | a60bd09ffda9c36443cf64c4c1f14d1c09442148 (diff) | |
download | luarocks-c01c8a21a735c3a74270635e5bd843bac71bc624.tar.gz luarocks-c01c8a21a735c3a74270635e5bd843bac71bc624.tar.bz2 luarocks-c01c8a21a735c3a74270635e5bd843bac71bc624.zip |
Tests: patch.lua
-rw-r--r-- | spec/fixtures/invalid_patch.patch | 20 | ||||
-rw-r--r-- | spec/fixtures/lao | 11 | ||||
-rw-r--r-- | spec/fixtures/tzu | 13 | ||||
-rw-r--r-- | spec/fixtures/valid_patch.patch | 19 | ||||
-rw-r--r-- | spec/tools_spec.lua | 88 | ||||
-rw-r--r-- | src/luarocks/tools/patch.lua | 41 |
6 files changed, 152 insertions, 40 deletions
diff --git a/spec/fixtures/invalid_patch.patch b/spec/fixtures/invalid_patch.patch new file mode 100644 index 00000000..75d05c58 --- /dev/null +++ b/spec/fixtures/invalid_patch.patch | |||
@@ -0,0 +1,20 @@ | |||
1 | --- lao 2002-02-21 23:30:39.942229878 -0800 | ||
2 | +++ tzu 2002-02-21 23:30:50.442260588 -0800 | ||
3 | @@ -1,7 +1,6 @@ | ||
4 | -The Way that can be told of is not the eternal Way; | ||
5 | -The name that can be named is not the eternal name. | ||
6 | The Nameless is the origin of Heaven and Earth; | ||
7 | -The Named is the mother of all things. | ||
8 | +The named is the mother of all things. | ||
9 | + | ||
10 | ? | ||
11 | Therefore let there always be non-being, | ||
12 | so we may see their subtlety, | ||
13 | And let there always be being, | ||
14 | @@ -9,3 +8,7 @@ | ||
15 | The two are the same, | ||
16 | But after they are produced, | ||
17 | they have different names. | ||
18 | +They both may be called deep and profound. | ||
19 | +Deeper and more profound, | ||
20 | +The door of all subtleties! | ||
diff --git a/spec/fixtures/lao b/spec/fixtures/lao new file mode 100644 index 00000000..635ef2c4 --- /dev/null +++ b/spec/fixtures/lao | |||
@@ -0,0 +1,11 @@ | |||
1 | The Way that can be told of is not the eternal Way; | ||
2 | The name that can be named is not the eternal name. | ||
3 | The Nameless is the origin of Heaven and Earth; | ||
4 | The Named is the mother of all things. | ||
5 | Therefore let there always be non-being, | ||
6 | so we may see their subtlety, | ||
7 | And let there always be being, | ||
8 | so we may see their outcome. | ||
9 | The two are the same, | ||
10 | But after they are produced, | ||
11 | they have different names. | ||
diff --git a/spec/fixtures/tzu b/spec/fixtures/tzu new file mode 100644 index 00000000..5af88a8f --- /dev/null +++ b/spec/fixtures/tzu | |||
@@ -0,0 +1,13 @@ | |||
1 | The Nameless is the origin of Heaven and Earth; | ||
2 | The named is the mother of all things. | ||
3 | |||
4 | Therefore let there always be non-being, | ||
5 | so we may see their subtlety, | ||
6 | And let there always be being, | ||
7 | so we may see their outcome. | ||
8 | The two are the same, | ||
9 | But after they are produced, | ||
10 | they have different names. | ||
11 | They both may be called deep and profound. | ||
12 | Deeper and more profound, | ||
13 | The door of all subtleties! | ||
diff --git a/spec/fixtures/valid_patch.patch b/spec/fixtures/valid_patch.patch new file mode 100644 index 00000000..042c7d22 --- /dev/null +++ b/spec/fixtures/valid_patch.patch | |||
@@ -0,0 +1,19 @@ | |||
1 | --- lao 2002-02-21 23:30:39.942229878 -0800 | ||
2 | +++ tzu 2002-02-21 23:30:50.442260588 -0800 | ||
3 | @@ -1,7 +1,6 @@ | ||
4 | -The Way that can be told of is not the eternal Way; | ||
5 | -The name that can be named is not the eternal name. | ||
6 | The Nameless is the origin of Heaven and Earth; | ||
7 | -The Named is the mother of all things. | ||
8 | +The named is the mother of all things. | ||
9 | + | ||
10 | Therefore let there always be non-being, | ||
11 | so we may see their subtlety, | ||
12 | And let there always be being, | ||
13 | @@ -9,3 +8,6 @@ | ||
14 | The two are the same, | ||
15 | But after they are produced, | ||
16 | they have different names. | ||
17 | +They both may be called deep and profound. | ||
18 | +Deeper and more profound, | ||
19 | +The door of all subtleties! | ||
diff --git a/spec/tools_spec.lua b/spec/tools_spec.lua new file mode 100644 index 00000000..7717be08 --- /dev/null +++ b/spec/tools_spec.lua | |||
@@ -0,0 +1,88 @@ | |||
1 | local test_env = require("spec.util.test_env") | ||
2 | local testing_paths = test_env.testing_paths | ||
3 | local get_tmp_path = test_env.get_tmp_path | ||
4 | local write_file = test_env.write_file | ||
5 | |||
6 | test_env.unload_luarocks() | ||
7 | local fs = require("luarocks.fs") | ||
8 | local patch = package.loaded["luarocks.tools.patch"] | ||
9 | |||
10 | describe("Luarocks patch test #unit", function() | ||
11 | local runner | ||
12 | |||
13 | setup(function() | ||
14 | runner = require("luacov.runner") | ||
15 | runner.init(testing_paths.testrun_dir .. "/luacov.config") | ||
16 | runner.tick = true | ||
17 | end) | ||
18 | |||
19 | teardown(function() | ||
20 | runner.shutdown() | ||
21 | end) | ||
22 | |||
23 | describe("patch.read_patch", function() | ||
24 | it("returns a table with the patch file info and the result of parsing the file", function() | ||
25 | local t, result | ||
26 | |||
27 | t, result = patch.read_patch(testing_paths.fixtures_dir .. "/valid_patch.patch") | ||
28 | assert.truthy(result) | ||
29 | |||
30 | t, result = patch.read_patch(testing_paths.fixtures_dir .. "/invalid_patch.patch") | ||
31 | assert.falsy(result) | ||
32 | end) | ||
33 | end) | ||
34 | |||
35 | describe("patch.apply_patch", function() | ||
36 | local tmpdir | ||
37 | local olddir | ||
38 | |||
39 | before_each(function() | ||
40 | tmpdir = get_tmp_path() | ||
41 | olddir = lfs.currentdir() | ||
42 | lfs.mkdir(tmpdir) | ||
43 | lfs.chdir(tmpdir) | ||
44 | |||
45 | local fd = assert(io.open(testing_paths.fixtures_dir .. "/lao")) | ||
46 | local laocontent = assert(fd:read("*a")) | ||
47 | fd:close() | ||
48 | write_file("lao", laocontent, finally) | ||
49 | |||
50 | fd = assert(io.open(testing_paths.fixtures_dir .. "/tzu")) | ||
51 | local tzucontent = assert(fd:read("*a")) | ||
52 | fd:close() | ||
53 | write_file("tzu", tzucontent, finally) | ||
54 | end) | ||
55 | |||
56 | after_each(function() | ||
57 | if olddir then | ||
58 | lfs.chdir(olddir) | ||
59 | if tmpdir then | ||
60 | lfs.rmdir(tmpdir) | ||
61 | end | ||
62 | end | ||
63 | end) | ||
64 | |||
65 | it("applies the given patch and returns true if the patch is valid", function() | ||
66 | local p = patch.read_patch(testing_paths.fixtures_dir .. "/valid_patch.patch") | ||
67 | local result = patch.apply_patch(p) | ||
68 | assert.truthy(result) | ||
69 | end) | ||
70 | |||
71 | it("returns false if the files to be patched are not valid or doesn't exist", function() | ||
72 | os.remove("lao") | ||
73 | os.remove("tzu") | ||
74 | local p = patch.read_patch(testing_paths.fixtures_dir .. "/invalid_patch.patch") | ||
75 | local result = patch.apply_patch(p) | ||
76 | assert.falsy(result) | ||
77 | end) | ||
78 | |||
79 | it("returns false if the target file is already patched", function() | ||
80 | local p = patch.read_patch(testing_paths.fixtures_dir .. "/valid_patch.patch") | ||
81 | local result = patch.apply_patch(p) | ||
82 | assert.truthy(result) | ||
83 | |||
84 | result = patch.apply_patch(p) | ||
85 | assert.falsy(result) | ||
86 | end) | ||
87 | end) | ||
88 | end) | ||
diff --git a/src/luarocks/tools/patch.lua b/src/luarocks/tools/patch.lua index 2e95e879..b46bd1d0 100644 --- a/src/luarocks/tools/patch.lua +++ b/src/luarocks/tools/patch.lua | |||
@@ -55,35 +55,6 @@ local function exists(filename) | |||
55 | end | 55 | end |
56 | local function isfile() return true end --FIX? | 56 | local function isfile() return true end --FIX? |
57 | 57 | ||
58 | local function read_file(filename) | ||
59 | local fh, data, err, oserr | ||
60 | fh, err, oserr = io.open(filename, 'rb') | ||
61 | if not fh then return fh, err, oserr end | ||
62 | data, err, oserr = fh:read'*a' | ||
63 | fh:close() | ||
64 | if not data then return nil, err, oserr end | ||
65 | return data | ||
66 | end | ||
67 | |||
68 | local function write_file(filename, data) | ||
69 | local fh, status, err, oserr | ||
70 | fh, err, oserr = io.open(filename 'wb') | ||
71 | if not fh then return fh, err, oserr end | ||
72 | status, err, oserr = fh:write(data) | ||
73 | fh:close() | ||
74 | if not status then return nil, err, oserr end | ||
75 | return true | ||
76 | end | ||
77 | |||
78 | local function file_copy(src, dest) | ||
79 | local data, status, err, oserr | ||
80 | data, err, oserr = read_file(src) | ||
81 | if not data then return data, err, oserr end | ||
82 | status, err, oserr = write_file(dest) | ||
83 | if not status then return status, err, oserr end | ||
84 | return true | ||
85 | end | ||
86 | |||
87 | local function string_as_file(s) | 58 | local function string_as_file(s) |
88 | return { | 59 | return { |
89 | at = 0, | 60 | at = 0, |
@@ -720,17 +691,7 @@ local function patch_file(source, target, epoch, hunks, strip, create_delete) | |||
720 | warning(format("failed backing up %s when patching", source)) | 691 | warning(format("failed backing up %s when patching", source)) |
721 | return false | 692 | return false |
722 | end | 693 | end |
723 | ok = patch_hunks(backupname, source, hunks) | 694 | patch_hunks(backupname, source, hunks) |
724 | if not ok then | ||
725 | warning(format("error patching file %s", source)) | ||
726 | if file_copy(source, source .. ".invalid") then | ||
727 | warning(format("invalid version is saved to %s", | ||
728 | source .. ".invalid")) | ||
729 | -- todo: proper rejects | ||
730 | os.rename(backupname, source) | ||
731 | end | ||
732 | return false | ||
733 | end | ||
734 | info(format("successfully patched %s", source)) | 695 | info(format("successfully patched %s", source)) |
735 | os.remove(backupname) | 696 | os.remove(backupname) |
736 | return true | 697 | return true |