Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug for interactive crate descr ending with " #1495

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: {}
Loading