[parted-devel] [PATCH] Fix the script mode for mkpart and mkpartfs.

Joel Andres Granados jgranado at redhat.com
Fri Jun 20 09:37:41 UTC 2008


In scripting mode, parted used to ask the user for confirmation
when the values to be used where not the ones specified by the user.
* parted/parted.c (do_mkpart, do_mkpartfs): if opt_script_mode is.
  set fail, if it's not, warn and ask for intervention.
* tests/Makefile.am : include the new test in the TEST list.
* tests/t7000-scripting.sh : Distribute new test case.
---
 parted/parted.c          |   36 +++++++++++----
 tests/Makefile.am        |    3 +-
 tests/t7000-scripting.sh |  106 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 134 insertions(+), 11 deletions(-)
 create mode 100755 tests/t7000-scripting.sh

diff --git a/parted/parted.c b/parted/parted.c
index 9f79ea4..28357f4 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -776,14 +776,24 @@ do_mkpart (PedDevice** dev)
                         start_sol = ped_unit_format (*dev, part->geom.start);
                         end_sol   = ped_unit_format (*dev, part->geom.end);
 
+                        /* In script mode failure to use specified values is fatal.
+                         * However, in interactive mode, it merely elicits a warning
+                         * and a prompt for whether to proceed.  The same appies for
+                         * do_mkpartfs function.
+                         */
                         switch (ped_exception_throw (
-                                PED_EXCEPTION_WARNING,
-                                PED_EXCEPTION_YES_NO,
+                                (opt_script_mode
+                                 ? PED_EXCEPTION_ERROR
+                                 : PED_EXCEPTION_WARNING),
+                                (opt_script_mode
+                                 ? PED_EXCEPTION_CANCEL
+                                 : PED_EXCEPTION_YES_NO),
                                 _("You requested a partition from %s to %s.\n"
                                   "The closest location we can manage is "
-                                  "%s to %s.  "
-                                  "Is this still acceptable to you?"),
-                                start_usr, end_usr, start_sol, end_sol))
+                                  "%s to %s.%s"),
+                                start_usr, end_usr, start_sol, end_sol,
+                                (opt_script_mode ? ""
+                                 : _("\nIs this still acceptable to you?"))))
                         {
                                 case PED_EXCEPTION_YES:
                                         /* all is well in this state */
@@ -937,13 +947,19 @@ do_mkpartfs (PedDevice** dev)
                         end_sol   = ped_unit_format (*dev, part->geom.end);
 
                         switch (ped_exception_throw (
-                                PED_EXCEPTION_WARNING,
-                                PED_EXCEPTION_YES_NO,
+                                (opt_script_mode
+                                 ? PED_EXCEPTION_ERROR
+                                 : PED_EXCEPTION_WARNING),
+                                (opt_script_mode
+                                 ? PED_EXCEPTION_CANCEL
+                                 : PED_EXCEPTION_YES_NO),
                                 _("You requested a partition from %s to %s.\n"
                                   "The closest location we can manage is "
-                                  "%s to %s.  "
-                                  "Is this still acceptable to you?"),
-                                start_usr, end_usr, start_sol, end_sol)) {
+                                  "%s to %s.%s"),
+                                start_usr, end_usr, start_sol, end_sol,
+                                (opt_script_mode ? ""
+                                 : _("\nIs this still acceptable to you?"))))
+                        {
                                 case PED_EXCEPTION_YES:
                                         /* all is well in this state */
                                         break;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ebebf49..fbcad7c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -12,7 +12,8 @@ TESTS = \
   t4100-msdos-partition-limits.sh \
   t4100-dvh-partition-limits.sh \
   t5000-tags.sh \
-  t6000-dm.sh
+  t6000-dm.sh \
+  t7000-scripting.sh
 
 EXTRA_DIST = \
   $(TESTS) test-lib.sh lvm-utils.sh
diff --git a/tests/t7000-scripting.sh b/tests/t7000-scripting.sh
new file mode 100755
index 0000000..5022f91
--- /dev/null
+++ b/tests/t7000-scripting.sh
@@ -0,0 +1,106 @@
+#!/bin/sh
+
+# Copyright (C) 2008 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+test_description='Make sure the scripting option works (-s) properly.'
+
+: ${srcdir=.}
+. $srcdir/test-lib.sh
+
+# The failure messages.
+cat << EOF >> errS || fail=1
+Error: You requested a partition from 512B to 50.7kB.
+The closest location we can manage is 17.4kB to 33.8kB.
+EOF
+
+cat << EOF >> errI || fail=1
+Warning: You requested a partition from 512B to 50.7kB.
+The closest location we can manage is 17.4kB to 33.8kB.
+Is this still acceptable to you?
+EOF
+echo -n "Yes/No? " >> errI
+
+# Test for mkpart in scripting mode
+test_expect_success \
+    'Create the test file' \
+    'dd if=/dev/zero of=testfile bs=512 count=100 2> /dev/null'
+
+test_expect_failure \
+    'Test the scripting mode of mkpart' \
+    'parted -s testfile "mklabel gpt mkpart primary ext3 1s -1s" > outS'
+
+test_expect_success \
+    'Compare the real error and the expected one' \
+    '$compare outS errS'
+
+# Test for mkpart in interactive mode.
+test_expect_success \
+    'Create the test file' \
+    '
+    rm testfile ;
+    dd if=/dev/zero of=testfile bs=512 count=100 2> /dev/null
+    '
+test_expect_failure \
+    'Test the interactive mode of mkpart' \
+    'echo n | \
+    parted ---pretend-input-tty testfile \
+    "mklabel gpt mkpart primary ext3 1s -1s" > outI
+    '
+# We have to format the output before comparing.
+test_expect_success \
+    'normilize the output' \
+    'sed -e "s,^.*Warning,Warning," -e "s,^.*Yes/No,Yes/No," -i outI'
+
+test_expect_success \
+    'Compare the real error and the expected one' \
+    '$compare outI errI'
+
+# Test for mkpartfs in scripting mode
+test_expect_success \
+    'Create the test file' \
+    'dd if=/dev/zero of=testfile bs=512 count=100 2> /dev/null'
+
+test_expect_failure \
+    'Test the scripting mode of mkpartfs' \
+    'parted -s testfile "mklabel gpt mkpartfs primary ext3 1s -1s" > outS'
+
+test_expect_success \
+    'Compare the real error and the expected one' \
+    '$compare outS errS'
+
+# Test for mkpartfs in interactive mode.
+test_expect_success \
+    'Create the test file' \
+    '
+    rm testfile ;
+    dd if=/dev/zero of=testfile bs=512 count=100 2> /dev/null
+    '
+test_expect_failure \
+    'Test the interactive mode of mkpartfs' \
+    'echo n | \
+    parted ---pretend-input-tty testfile \
+    "mklabel gpt mkpartfs primary ext3 1s -1s" > outI
+    '
+# We have to format the output before comparing.
+test_expect_success \
+    'normilize the output' \
+    'sed -e "s,^.*Warning,Warning," -e "s,^.*Yes/No,Yes/No," -i outI'
+
+test_expect_success \
+    'Compare the real error and the expected one' \
+    '$compare outI errI'
+
+test_done
-- 
1.5.4.1




More information about the parted-devel mailing list