Skip to content
This repository was archived by the owner on May 4, 2024. It is now read-only.

Commit 9414e8a

Browse files
Refactor movey-login logic, refactor movey tests
1 parent 6a505b0 commit 9414e8a

File tree

7 files changed

+80
-128
lines changed

7 files changed

+80
-128
lines changed

language/move-command-line-common/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub mod address;
88
pub mod character_sets;
99
pub mod env;
1010
pub mod files;
11-
pub mod movey;
11+
pub mod movey_constants;
1212
pub mod parser;
1313
pub mod testing;
1414
pub mod types;

language/tools/move-cli/src/base/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub mod disassemble;
77
pub mod docgen;
88
pub mod errmap;
99
pub mod info;
10+
pub mod movey_login;
1011
pub mod new;
1112
pub mod prove;
1213
pub mod test;

language/tools/move-cli/src/movey_login/cli.rs renamed to language/tools/move-cli/src/base/movey_login.rs

Lines changed: 68 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -3,100 +3,85 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
use anyhow::{bail, Result};
6-
use move_command_line_common::{env::MOVE_HOME, movey::MOVEY_URL};
6+
use clap::Parser;
7+
use move_command_line_common::{env::MOVE_HOME, movey_constants::MOVEY_URL};
78
use std::{fs, fs::File, io, path::PathBuf};
89
use toml_edit::easy::{map::Map, Value};
910

10-
pub const MOVEY_API_KEY_PATH: &str = "/movey_api_key.toml";
11-
12-
pub struct TestMode {
13-
pub test_path: String,
14-
}
15-
16-
pub fn handle_movey_login_commands(test_path: Option<String>) -> Result<()> {
17-
println!(
18-
"Please paste the API Token found on {}/settings/tokens below",
19-
MOVEY_URL
20-
);
21-
let mut line = String::new();
22-
loop {
23-
match io::stdin().read_line(&mut line) {
24-
Ok(_) => {
25-
if let Some('\n') = line.chars().next_back() {
26-
line.pop();
11+
pub const MOVEY_CREDENTIAL_PATH: &str = "/movey_credential.toml";
12+
13+
#[derive(Parser)]
14+
#[clap(name = "movey-login")]
15+
pub struct MoveyLogin;
16+
17+
impl MoveyLogin {
18+
pub fn execute(self) -> Result<()> {
19+
println!(
20+
"Please paste the API Token found on {}/settings/tokens below",
21+
MOVEY_URL
22+
);
23+
let mut line = String::new();
24+
loop {
25+
match io::stdin().read_line(&mut line) {
26+
Ok(_) => {
27+
line = line.trim().to_string();
28+
if !line.is_empty() {
29+
break;
30+
}
31+
println!("Invalid API Token. Try again!");
2732
}
28-
if let Some('\r') = line.chars().next_back() {
29-
line.pop();
33+
Err(err) => {
34+
bail!("Error reading file: {}", err);
3035
}
31-
if !line.is_empty() {
32-
break;
33-
}
34-
println!("Invalid API Token. Try again!");
35-
}
36-
Err(err) => {
37-
bail!("Error reading file: {}", err);
3836
}
3937
}
38+
Self::save_credential(line, MOVE_HOME.clone())?;
39+
println!("Token for Movey saved.");
40+
Ok(())
4041
}
41-
let mut test_mode: Option<TestMode> = None;
42-
if let Some(path) = test_path {
43-
test_mode = Some(TestMode { test_path: path });
44-
}
45-
save_credential(line, test_mode)?;
46-
println!("Token for Movey saved.");
47-
Ok(())
48-
}
4942

50-
pub fn save_credential(token: String, test_mode: Option<TestMode>) -> Result<()> {
51-
let mut move_home;
52-
if let Some(test_mode) = test_mode {
53-
move_home = std::env::var("TEST_MOVE_HOME").unwrap();
54-
if !test_mode.test_path.is_empty() {
55-
move_home.push_str(&test_mode.test_path);
43+
pub fn save_credential(token: String, move_home: String) -> Result<()> {
44+
fs::create_dir_all(&move_home)?;
45+
let credential_path = move_home + MOVEY_CREDENTIAL_PATH;
46+
let credential_file = PathBuf::from(&credential_path);
47+
if !credential_file.exists() {
48+
let credential_file = File::create(&credential_path)?;
49+
set_permissions(&credential_file, 0o600)?;
5650
}
57-
} else {
58-
move_home = MOVE_HOME.clone();
59-
}
60-
fs::create_dir_all(&move_home)?;
61-
let credential_path = move_home + MOVEY_API_KEY_PATH;
62-
let credential_file = PathBuf::from(&credential_path);
63-
if !credential_file.exists() {
64-
File::create(&credential_path)?;
65-
}
6651

67-
let old_contents: String;
68-
match fs::read_to_string(&credential_path) {
69-
Ok(contents) => {
70-
old_contents = contents;
52+
let old_contents: String;
53+
match fs::read_to_string(&credential_path) {
54+
Ok(contents) => {
55+
old_contents = contents;
56+
}
57+
Err(error) => bail!("Error reading input: {}", error),
7158
}
72-
Err(error) => bail!("Error reading input: {}", error),
73-
}
74-
let mut toml: Value = old_contents
75-
.parse()
76-
.map_err(|e| anyhow::Error::from(e).context("could not parse input as TOML"))?;
77-
78-
if let Some(registry) = toml.as_table_mut().unwrap().get_mut("registry") {
79-
if let Some(toml_token) = registry.as_table_mut().unwrap().get_mut("token") {
80-
*toml_token = Value::String(token);
59+
let mut toml: Value = old_contents
60+
.parse()
61+
.map_err(|e| anyhow::Error::from(e).context("could not parse input as TOML"))?;
62+
63+
// only update token key, keep the rest of the file intact
64+
if let Some(registry) = toml.as_table_mut().unwrap().get_mut("registry") {
65+
if let Some(toml_token) = registry.as_table_mut().unwrap().get_mut("token") {
66+
*toml_token = Value::String(token);
67+
} else {
68+
registry
69+
.as_table_mut()
70+
.unwrap()
71+
.insert(String::from("token"), Value::String(token));
72+
}
8173
} else {
82-
registry
83-
.as_table_mut()
74+
let mut value = Map::new();
75+
value.insert(String::from("token"), Value::String(token));
76+
toml.as_table_mut()
8477
.unwrap()
85-
.insert(String::from("token"), Value::String(token));
78+
.insert(String::from("registry"), Value::Table(value));
8679
}
87-
} else {
88-
let mut value = Map::new();
89-
value.insert(String::from("token"), Value::String(token));
90-
toml.as_table_mut()
91-
.unwrap()
92-
.insert(String::from("registry"), Value::Table(value));
93-
}
9480

95-
let new_contents = toml.to_string();
96-
fs::write(credential_file, new_contents).expect("Unable to write file");
97-
let file = File::open(&credential_path)?;
98-
set_permissions(&file, 0o600)?;
99-
Ok(())
81+
let new_contents = toml.to_string();
82+
fs::write(credential_file, new_contents).expect("Unable to write file");
83+
Ok(())
84+
}
10085
}
10186

10287
#[cfg(unix)]
@@ -123,13 +108,12 @@ mod tests {
123108
fn setup_move_home(test_path: &str) -> (String, String) {
124109
let cwd = env::current_dir().unwrap();
125110
let mut move_home: String = String::from(cwd.to_string_lossy());
126-
env::set_var("TEST_MOVE_HOME", &move_home);
127111
if !test_path.is_empty() {
128112
move_home.push_str(&test_path);
129113
} else {
130114
move_home.push_str("/test");
131115
}
132-
let credential_path = move_home.clone() + MOVEY_API_KEY_PATH;
116+
let credential_path = move_home.clone() + MOVEY_CREDENTIAL_PATH;
133117
(move_home, credential_path)
134118
}
135119

@@ -142,11 +126,7 @@ mod tests {
142126
let (move_home, credential_path) =
143127
setup_move_home("/save_credential_works_if_no_credential_file_exists");
144128
let _ = fs::remove_dir_all(&move_home);
145-
146-
let test_mode = Some(TestMode {
147-
test_path: String::from("/save_credential_works_if_no_credential_file_exists"),
148-
});
149-
save_credential(String::from("test_token"), test_mode).unwrap();
129+
MoveyLogin::save_credential(String::from("test_token"), move_home.clone()).unwrap();
150130

151131
let contents = fs::read_to_string(&credential_path).expect("Unable to read file");
152132
let mut toml: Value = contents.parse().unwrap();
@@ -170,10 +150,7 @@ mod tests {
170150
let mut toml: Value = contents.parse().unwrap();
171151
assert!(toml.as_table_mut().unwrap().get_mut("registry").is_none());
172152

173-
let test_mode = Some(TestMode {
174-
test_path: String::from("/save_credential_works_if_empty_credential_file_exists"),
175-
});
176-
save_credential(String::from("test_token"), test_mode).unwrap();
153+
MoveyLogin::save_credential(String::from("test_token"), move_home.clone()).unwrap();
177154

178155
let contents = fs::read_to_string(&credential_path).expect("Unable to read file");
179156
let mut toml: Value = contents.parse().unwrap();
@@ -204,10 +181,7 @@ mod tests {
204181
assert!(token.to_string().contains("old_test_token"));
205182
assert!(!token.to_string().contains("new_world"));
206183

207-
let test_mode = Some(TestMode {
208-
test_path: String::from("/save_credential_works_if_token_field_exists"),
209-
});
210-
save_credential(String::from("new_world"), test_mode).unwrap();
184+
MoveyLogin::save_credential(String::from("new_world"), move_home.clone()).unwrap();
211185

212186
let contents = fs::read_to_string(&credential_path).expect("Unable to read file");
213187
let mut toml: Value = contents.parse().unwrap();
@@ -239,10 +213,7 @@ mod tests {
239213
let token = registry.as_table_mut().unwrap().get_mut("token").unwrap();
240214
assert!(!token.to_string().contains("test_token"));
241215

242-
let test_mode = Some(TestMode {
243-
test_path: String::from("/save_credential_works_if_empty_token_field_exists"),
244-
});
245-
save_credential(String::from("test_token"), test_mode).unwrap();
216+
MoveyLogin::save_credential(String::from("test_token"), move_home.clone()).unwrap();
246217

247218
let contents = fs::read_to_string(&credential_path).expect("Unable to read file");
248219
let mut toml: Value = contents.parse().unwrap();

language/tools/move-cli/src/lib.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
use base::{
66
build::Build, coverage::Coverage, disassemble::Disassemble, docgen::Docgen, errmap::Errmap,
7-
info::Info, new::New, prove::Prove, test::Test,
7+
info::Info, movey_login::MoveyLogin, new::New, prove::Prove, test::Test,
88
};
99
use move_package::BuildConfig;
1010

1111
pub mod base;
1212
pub mod experimental;
13-
pub mod movey_login;
1413
pub mod sandbox;
1514

1615
/// Default directory where saved Move resources live
@@ -93,10 +92,7 @@ pub enum Command {
9392
cmd: experimental::cli::ExperimentalCommand,
9493
},
9594
#[clap(name = "movey-login")]
96-
MoveyLogin {
97-
#[clap(long = "test-path")]
98-
test_path: Option<String>,
99-
},
95+
MoveyLogin(MoveyLogin),
10096
}
10197

10298
pub fn run_cli(
@@ -127,9 +123,7 @@ pub fn run_cli(
127123
&storage_dir,
128124
),
129125
Command::Experimental { storage_dir, cmd } => cmd.handle_command(&move_args, &storage_dir),
130-
Command::MoveyLogin { test_path } => {
131-
movey_login::cli::handle_movey_login_commands(test_path)
132-
}
126+
Command::MoveyLogin(c) => c.execute(),
133127
}
134128
}
135129

language/tools/move-cli/src/movey_login/mod.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

language/tools/move-cli/tests/cli_tests.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Copyright (c) The Move Contributors
33
// SPDX-License-Identifier: Apache-2.0
44

5-
use move_cli::{movey_login::cli::MOVEY_API_KEY_PATH, sandbox::commands::test};
6-
use move_command_line_common::movey::MOVEY_URL;
5+
use move_cli::{base::movey_login::MOVEY_CREDENTIAL_PATH, sandbox::commands::test};
6+
use move_command_line_common::movey_constants::MOVEY_URL;
77
#[cfg(unix)]
88
use std::fs::File;
99
use std::{env, fs, io::Write};
@@ -68,13 +68,9 @@ fn save_credential_works() {
6868
assert!(fs::read_to_string(&credential_path).is_err());
6969

7070
match std::process::Command::new(cli_exe)
71+
.env("MOVE_HOME", &move_home)
7172
.current_dir(".")
72-
.args([
73-
"movey-login",
74-
"--test",
75-
"--test-path",
76-
"/save_credential_works",
77-
])
73+
.args(["movey-login"])
7874
.stdin(Stdio::piped())
7975
.stdout(Stdio::piped())
8076
.spawn()
@@ -123,13 +119,9 @@ fn save_credential_fails_if_undeletable_credential_file_exists() {
123119
file.set_permissions(perms).unwrap();
124120

125121
match std::process::Command::new(cli_exe)
122+
.env("MOVE_HOME", &move_home)
126123
.current_dir(".")
127-
.args([
128-
"movey-login",
129-
"--test",
130-
"--test-path",
131-
"/save_credential_fails_if_undeletable_credential_file_exists",
132-
])
124+
.args(["movey-login"])
133125
.stdin(Stdio::piped())
134126
.stdout(Stdio::piped())
135127
.stderr(Stdio::piped())
@@ -171,11 +163,10 @@ fn save_credential_fails_if_undeletable_credential_file_exists() {
171163
fn setup_move_home(test_path: &str) -> (String, String) {
172164
let cwd = env::current_dir().unwrap();
173165
let mut move_home: String = String::from(cwd.to_string_lossy());
174-
env::set_var("TEST_MOVE_HOME", &move_home);
175166
move_home.push_str(&test_path);
176167
let _ = fs::remove_dir_all(&move_home);
177168
fs::create_dir_all(&move_home).unwrap();
178-
let credential_path = move_home.clone() + MOVEY_API_KEY_PATH;
169+
let credential_path = move_home.clone() + MOVEY_CREDENTIAL_PATH;
179170
(move_home, credential_path)
180171
}
181172

0 commit comments

Comments
 (0)