|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# MIT License |
| 4 | +# |
| 5 | +# Copyright The SCons Foundation |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining |
| 8 | +# a copy of this software and associated documentation files (the |
| 9 | +# "Software"), to deal in the Software without restriction, including |
| 10 | +# without limitation the rights to use, copy, modify, merge, publish, |
| 11 | +# distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | +# permit persons to whom the Software is furnished to do so, subject to |
| 13 | +# the following conditions: |
| 14 | +# |
| 15 | +# The above copyright notice and this permission notice shall be included |
| 16 | +# in all copies or substantial portions of the Software. |
| 17 | +# |
| 18 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY |
| 19 | +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 20 | +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 22 | +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 23 | +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 24 | +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | + |
| 26 | +""" |
| 27 | +Verify that when an option is specified with nargs > 1, |
| 28 | +SCons consumes those correctly into the args. |
| 29 | +""" |
| 30 | + |
| 31 | +import TestSCons |
| 32 | + |
| 33 | +test = TestSCons.TestSCons() |
| 34 | + |
| 35 | +# First, test an option with nargs=2 and no others: |
| 36 | +test.write( |
| 37 | + 'SConstruct', |
| 38 | + """\ |
| 39 | +env = Environment() |
| 40 | +AddOption('--extras', |
| 41 | + nargs=2, |
| 42 | + dest='extras', |
| 43 | + action='store', |
| 44 | + type='string', |
| 45 | + metavar='FILE1 FILE2', |
| 46 | + default=(), |
| 47 | + help='two extra files to install') |
| 48 | +print(str(GetOption('extras'))) |
| 49 | +""", |
| 50 | +) |
| 51 | + |
| 52 | +# no args |
| 53 | +test.run('-Q -q .', stdout="()\n") |
| 54 | +# one arg, should fail |
| 55 | +test.run( |
| 56 | + '-Q -q . --extras A', |
| 57 | + status=2, |
| 58 | + stderr="""\ |
| 59 | +usage: scons [OPTION] [TARGET] ... |
| 60 | +
|
| 61 | +SCons Error: --extras option requires 2 arguments |
| 62 | +""", |
| 63 | +) |
| 64 | +# two args |
| 65 | +test.run('-Q -q . --extras A B', status=1, stdout="('A', 'B')\n") |
| 66 | +# -- means the rest are not processed as args |
| 67 | +test.run('-Q -q . -- --extras A B', status=1, stdout="()\n") |
| 68 | + |
| 69 | +# Now test what has been a bug: another option is |
| 70 | +# also defined, this impacts the collection of args for the nargs>1 opt |
| 71 | +test.write( |
| 72 | + 'SConstruct', |
| 73 | + """\ |
| 74 | +env = Environment() |
| 75 | +AddOption( |
| 76 | + '--prefix', |
| 77 | + nargs=1, |
| 78 | + dest='prefix', |
| 79 | + action='store', |
| 80 | + type='string', |
| 81 | + metavar='DIR', |
| 82 | + help='installation prefix', |
| 83 | +) |
| 84 | +AddOption( |
| 85 | + '--extras', |
| 86 | + nargs=2, |
| 87 | + dest='extras', |
| 88 | + action='store', |
| 89 | + type='string', |
| 90 | + metavar='FILE1 FILE2', |
| 91 | + default=(), |
| 92 | + help='two extra files to install', |
| 93 | +) |
| 94 | +print(str(GetOption('prefix'))) |
| 95 | +print(str(GetOption('extras'))) |
| 96 | +""", |
| 97 | +) |
| 98 | + |
| 99 | +# no options |
| 100 | +test.run('-Q -q .', stdout="None\n()\n") |
| 101 | +# one single-arg option |
| 102 | +test.run('-Q -q . --prefix=/home/foo', stdout="/home/foo\n()\n") |
| 103 | +# one two-arg option |
| 104 | +test.run('-Q -q . --extras A B', status=1, stdout="None\n('A', 'B')\n") |
| 105 | +# single-arg option followed by two-arg option |
| 106 | +test.run( |
| 107 | + '-Q -q . --prefix=/home/foo --extras A B', |
| 108 | + status=1, |
| 109 | + stdout="/home/foo\n('A', 'B')\n", |
| 110 | +) |
| 111 | +# two-arg option followed by single-arg option |
| 112 | +test.run( |
| 113 | + '-Q -q . --extras A B --prefix=/home/foo', |
| 114 | + status=1, |
| 115 | + stdout="/home/foo\n('A', 'B')\n", |
| 116 | +) |
| 117 | + |
| 118 | +test.pass_test() |
| 119 | + |
| 120 | +# Local Variables: |
| 121 | +# tab-width:4 |
| 122 | +# indent-tabs-mode:nil |
| 123 | +# End: |
| 124 | +# vim: set expandtab tabstop=4 shiftwidth=4: |
0 commit comments