Skip to content

Commit

Permalink
Fix bug for interactive crate descr ending with "
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Nov 8, 2023
1 parent a69ac7c commit adcdf7c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 25 deletions.
56 changes: 31 additions & 25 deletions src/alr/alr-commands-init.adb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ with CLIC.TTY; use CLIC.TTY;

package body Alr.Commands.Init is

package TIO renames Ada.Wide_Wide_Text_IO;
package UI renames Alire.Utils.User_Input;

type Crate_Kind is (Library, Binary);
Expand All @@ -42,8 +43,6 @@ package body Alr.Commands.Init is
procedure Generate (Cmd : Command;
Info : Crate_Init_Info)
is

package TIO renames Ada.Wide_Wide_Text_IO;
use AAA.Strings;

For_Library : constant Boolean := Info.Kind = Library;
Expand Down Expand Up @@ -78,15 +77,22 @@ package body Alr.Commands.Init is
Table.Set ("key", TOML.Create_String (S));

-- Remove excess whitespace and quotation
return
Trim
(Trim
(Trim (Tail (TOML.Dump_As_String (Table), '=')),
ASCII.LF),
'"');
declare
Result : constant String :=
Trim
(Trim
(Tail (TOML.Dump_As_String (Table), '='),
ASCII.LF));
begin
-- Trimming the TOML quotes at the extremes fails for a
-- string with quotes at the extremes of the string because
-- Ada.Strings.Trim removes those too! So just remove the
-- quotes we know are there.
return Result (Result'First + 1 .. Result'Last - 1);
end;
end Escape;

function Q (S : String) return String is ("""" & Escape (S) & """");
function Q (S : String) return String is ('"' & Escape (S) & '"');
-- Quote string

function Q (S : Unbounded_String) return String
Expand Down Expand Up @@ -242,23 +248,7 @@ package body Alr.Commands.Init is
-----------------------

procedure Generate_Manifest is
use Alire.Config;
begin
if Builtins.User_Email.Is_Empty or else
Builtins.User_Name.Is_Empty or else
Builtins.User_Github_Login.Is_Empty
then
AAA.Text_IO.Put_Paragraph
("Alire needs some user information to initialize the crate"
& " author and maintainer, for eventual submission to"
& " the Alire community index. This information will be"
& " interactively requested now.");
TIO.New_Line;
TIO.Put_Line
("You can edit this information at any time with 'alr config'");
TIO.New_Line;
end if;

declare
-- Retrieve initial values from config or user. Only the name may
-- require encoding, as emails and logins cannot contain strange
Expand Down Expand Up @@ -590,13 +580,29 @@ package body Alr.Commands.Init is
procedure Execute (Cmd : in out Command;
Args : AAA.Strings.Vector)
is
use Alire.Config;
Info : Crate_Init_Info;
begin

if Cmd.Bin and then Cmd.Lib then
Reportaise_Wrong_Arguments ("Please provide either --bin or --lib");
end if;

if Builtins.User_Email.Is_Empty or else
Builtins.User_Name.Is_Empty or else
Builtins.User_Github_Login.Is_Empty
then
AAA.Text_IO.Put_Paragraph
("Alire needs some user information to initialize the crate"
& " author and maintainer, for eventual submission to"
& " the Alire community index. This information will be"
& " interactively requested now.");
TIO.New_Line;
TIO.Put_Line
("You can edit this information at any time with 'alr config'");
TIO.New_Line;
end if;

Query_Crate_Name (Args, Info);

if Cmd.Bin then
Expand Down
55 changes: 55 additions & 0 deletions testsuite/tests/init/interactive-inputs/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Validate tricky user inputs during crate initialization
"""

import os
import shutil

from drivers.alr import run_alr, run_alr_interactive
from drivers.asserts import assert_eq


def check(descr: str = "", name: str = ""):
"""
Initialize a crate with given description and name
"""
run_alr_interactive(['init', '--bin', 'xxx'],
output=['> ' for _ in range(7)],
input=[descr, # Description
name, # Full user name
'', # Github login
'', # Email
'', # License
'', # Tags
''], # Website
timeout=3)

# Check that it can be shown, which will load the manifest
os.chdir("xxx")
p = run_alr("show")

# Verify the description and author in output match the input

# Description is in first line after the ": "
assert_eq(p.out.splitlines()[0].split(": ", 1)[1], descr)

# Author is in the fourth line after the ": "
assert_eq(p.out.splitlines()[3].split(": ", 1)[1], name)

# Prepare for next iteration
os.chdir("..")
shutil.rmtree("xxx")

# Unset the user information so it is re-asked next time
run_alr('config', '--global', '--unset', 'user.name')
# Other fields are not being set because they use the defaults so don't
# require unsetting.


# Try a few tricky inputs
check('hello "world"', 'Robert "Bobby" Tables')
check('"hello" "world"', '"Bobby" Robert Tables')
check('""""""', '"Bobby" Robert "Tables"')


print('SUCCESS')
2 changes: 2 additions & 0 deletions testsuite/tests/init/interactive-inputs/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
driver: python-script
indexes: {}

0 comments on commit adcdf7c

Please sign in to comment.